OK I give up, how do I do this...
I have a DB that stores dynamic variables with I group into
group_id''s. For instance here are a couple examples of db rows:
title="name", description = "john", grouping_id =
"1"
title="location", description = "USA", grouping_id =
"1"
title="comment", description = "hello", grouping_id =
"1"
title="name", description = "frank", grouping_id =
"2"
title="location", description = "CA", grouping_id =
"2"
title="comment", description = "hi", grouping_id =
"2"
Sure I could create a DB with these columns, but for various reasons
I''m doing it this way.
SO, how do I store all this information in a hash? I want end up
looking something like this:
@vars = {:1 => { :name =>
"john", :location=>"USA",
:comment=>"hello"}, :2=>{ :name =>
"frank", :location=>"CA", :comment=>"hi"} }
I tried this:
@vars = Hash.new { Hash.new }
Mymodel.all.each do |a|
@vars["#{a.grouping_id}"] = { :"#{a.title}"
=>
a.description }
end
But that only stores the last variable in each grouping ID
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
h = {}
model.each do |r|
h[r.grouping_id] ||= {}
h[r.grouping_id][:name] = r.name
h[r.grouping_id][:description] = r.description
end
On Mon, Feb 22, 2010 at 11:08 AM, Brent
<wejrowski-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> OK I give up, how do I do this...
>
> I have a DB that stores dynamic variables with I group into
> group_id''s. For instance here are a couple examples of db rows:
>
> title="name", description = "john", grouping_id =
"1"
> title="location", description = "USA", grouping_id =
"1"
> title="comment", description = "hello", grouping_id =
"1"
>
> title="name", description = "frank", grouping_id =
"2"
> title="location", description = "CA", grouping_id =
"2"
> title="comment", description = "hi", grouping_id =
"2"
>
>
> Sure I could create a DB with these columns, but for various reasons
> I''m doing it this way.
>
> SO, how do I store all this information in a hash? I want end up
> looking something like this:
>
> @vars = {:1 => { :name =>
> "john", :location=>"USA",
:comment=>"hello"}, :2=>{ :name =>
> "frank", :location=>"CA",
:comment=>"hi"} }
>
>
> I tried this:
>
> @vars = Hash.new { Hash.new }
> Mymodel.all.each do |a|
> @vars["#{a.grouping_id}"] = { :"#{a.title}"
=>
> a.description }
> end
>
> But that only stores the last variable in each grouping ID
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Ahh beautiful.. part of your code didn''t work with the || = but I did
this:
@vars = {}
Model.all.each do |a|
if @vars[a.grouping_id] == nil
@vars[a.grouping_id] = {}
end
@vars[a.grouping_id][:"#{a.title}"] = a.description
end
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Brent wrote:> OK I give up, how do I do this... > > I have a DB that stores dynamic variables with I group into > group_id''s. For instance here are a couple examples of db rows: > > title="name", description = "john", grouping_id = "1" > title="location", description = "USA", grouping_id = "1" > title="comment", description = "hello", grouping_id = "1" > > title="name", description = "frank", grouping_id = "2" > title="location", description = "CA", grouping_id = "2" > title="comment", description = "hi", grouping_id = "2" > > > Sure I could create a DB with these columns, but for various reasons > I''m doing it this way.And those various reasons are...? In general, it''s best to use a database for your data store.> > SO, how do I store all this information in a hash? I want end up > looking something like this: > > @vars = {:1 => { :name => > "john", :location=>"USA", :comment=>"hello"}, :2=>{ :name => > "frank", :location=>"CA", :comment=>"hi"} } > > > I tried this: > > @vars = Hash.new { Hash.new } > Mymodel.all.each do |a| > @vars["#{a.grouping_id}"] = { :"#{a.title}" => > a.description } > end > > But that only stores the last variable in each grouping IDYou probably want an array of hashes, not a hash of hashes. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.