I have an array of Hash to be used for creating records in db with this format : geo_areas.first => {"id"=>1, "areaOrder"=>1, "areaName"=>"Europe", "areaCode"=>"EUR", "areaEnabled"=>1} I''ll loop over the geo_areas array to create all records .. but I need to use the ''id'' key as the :id of my records upon creation doing : GeoArea.new(geo_areas.first) doesn''t take the "id" in account => #<GeoArea id: nil, areaOrder: 1, areaName: "Europe", areaCode: "EUR", areaEnabled: true> and GeoArea.create(geo_areas.first) auto-generate the id , not taking in account the "id" in the Hash ... how should I proceed ? tfyi -- 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.
On Fri, Apr 22, 2011 at 9:26 PM, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I have an array of Hash to be used for creating records in db with > this format : > > geo_areas.first > => {"id"=>1, "areaOrder"=>1, "areaName"=>"Europe", "areaCode"=>"EUR", > "areaEnabled"=>1} > > I''ll loop over the geo_areas array to create all records .. but I need > to use the ''id'' key as the :id of my records upon creation > > doing : > GeoArea.new(geo_areas.first) doesn''t take the "id" in account > => #<GeoArea id: nil, areaOrder: 1, areaName: "Europe", areaCode: > "EUR", areaEnabled: true> > >You might want to look at the Fixtures class. I don''t know where I got this seeding script before but it uses fixtures to create the records. http://pastie.org/1822406> and > GeoArea.create(geo_areas.first) auto-generate the id , not taking in > account the "id" in the Hash ... > > how should I proceed ? > > tfyi > > -- > 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. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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.
Thanks Jim I am using a specific seed.rb script to seed the database from scratch , as it clean the tables it''s not so important, the id auto- generated in sequence. If the table is not empty (let''s assume 12 records) , then the first hash will have the id : 13 despite the fact that there is an "id" key in the hash .. I should take off the auto-generation of the id On 22 avr, 15:32, Jim Ruther Nill <jvn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Apr 22, 2011 at 9:26 PM, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > I have an array of Hash to be used for creating records in db with > > this format : > > > geo_areas.first > > => {"id"=>1, "areaOrder"=>1, "areaName"=>"Europe", "areaCode"=>"EUR", > > "areaEnabled"=>1} > > > I''ll loop over the geo_areas array to create all records .. but I need > > to use the ''id'' key as the :id of my records upon creation > > > doing : > > GeoArea.new(geo_areas.first) doesn''t take the "id" in account > > => #<GeoArea id: nil, areaOrder: 1, areaName: "Europe", areaCode: > > "EUR", areaEnabled: true> > > You might want to look at the Fixtures class. > > I don''t know where I got this seeding script before but it uses fixtures > to create the records.http://pastie.org/1822406 > > > > > > > and > > GeoArea.create(geo_areas.first) auto-generate the id , not taking in > > account the "id" in the Hash ... > > > how should I proceed ? > > > tfyi > > > -- > > 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. > > -- > ------------------------------------------------------------- > visit my blog athttp://jimlabs.heroku.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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Apr-22 19:00 UTC
Re: yaml dump of db to be used for creating records
On Apr 22, 2:26 pm, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote:> I have an array of Hash to be used for creating records in db with > this format : > > geo_areas.first > => {"id"=>1, "areaOrder"=>1, "areaName"=>"Europe", "areaCode"=>"EUR", > "areaEnabled"=>1} > > I''ll loop over the geo_areas array to create all records .. but I need > to use the ''id'' key as the :id of my records upon creation > > doing : > GeoArea.new(geo_areas.first) doesn''t take the "id" in account > => #<GeoArea id: nil, areaOrder: 1, areaName: "Europe", areaCode: > "EUR", areaEnabled: true> > > and > GeoArea.create(geo_areas.first) auto-generate the id , not taking in > account the "id" in the Hash ...I think the id attribute might be protected by default. Try area GeoArea.new(...); area.id = ...; area.save Fred> > how should I proceed ? > > tfyi-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Maybe this is a bug (or undocumented feature) in YAML. I have a similar problem in Rails (but ActiveRecord is not involved in this workflow): I YAML.dump a Hash that contains attributes that I want to pass to a MyModel#new method. But when I YAML.load the resulting yaml, the key :id is not in the Hash anymore! I tried to reproduce this behavior with a simpler Hash, but i wasn''t able to. My current workaround is to rename the key ":id" to ":_id" before dumping and rename it back after loading... Ephraim -- 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.