Chris Richards
2008-Feb-26  13:31 UTC
Overwrite primary key when importing with ActiveRecord
In my migrations I am importing data from a CSV file. So i first create the table : create_table :customers do |t| ... end and then import data from a CSV into it. The CSV data allready contains values for the id field. My problem is that activerecord won''t let me override the id field : Customer.create(:id=>1000, :name=>.........) It uses its own ID. Does anyone know how I can solve this the correct way? Any help is appreciated. Thanks Chris -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-Feb-26  13:42 UTC
Re: Overwrite primary key when importing with ActiveRecord
On Feb 26, 2008, at 8:31 AM, Chris Richards wrote:> In my migrations I am importing data from a CSV file. > > So i first create the table : > > create_table :customers do |t| > ... > end > > and then import data from a CSV into it. The CSV data allready > contains > values for the id field. My problem is that activerecord won''t let me > override the id field : > > Customer.create(:id=>1000, :name=>.........) > > It uses its own ID. > > Does anyone know how I can solve this the correct way? > > Any help is appreciated. > > Thanks > Chriscustomer = Customer.new(:name => ...) do |c| c.id = 1000 end customer.save You can''t set the id from the hash, but you can set it separately. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Richards
2008-Feb-26  13:47 UTC
Re: Overwrite primary key when importing with ActiveRecord
Thanks Rob, you''ve saved me writing some ugly code to get around it. What''s the rational behind that behavior? Chris> On Feb 26, 2008, at 8:31 AM, Chris Richards wrote: >> values for the id field. My problem is that activerecord won''t let me >> Thanks >> Chris > > customer = Customer.new(:name => ...) do |c| > c.id = 1000 > end > customer.save > > You can''t set the id from the hash, but you can set it separately. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-Feb-26  14:33 UTC
Re: Overwrite primary key when importing with ActiveRecord
Well, you normally expect the :id to be auto-assigned by the database  
so it is prohibited (ignored) in the hash.  Consider something like:
fred = Customer.find_by_name(''fred'')
barney = Customer.new(fred.attributes)
barney.save
As long as there are no other uniqueness constraints, barney should be  
saved and have his own id.
You can use attr_protected to pass this behavior to other columns of  
your choosing.
-Rob
On Feb 26, 2008, at 8:47 AM, Chris Richards wrote:
>
> Thanks Rob, you''ve saved me writing some ugly code to get around
it.
>
> What''s the rational behind that behavior?
>
> Chris
>
>> On Feb 26, 2008, at 8:31 AM, Chris Richards wrote:
>>> values for the id field.  My problem is that activerecord
won''t
>>> let me
>>> Thanks
>>> Chris
>>
>> customer = Customer.new(:name => ...) do |c|
>>   c.id = 1000
>> end
>> customer.save
>>
>> You can''t set the id from the hash, but you can set it
separately.
>>
>> -Rob
>>
>> Rob Biedenharn    http://agileconsultingllc.com
>> Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---