Hi folx, may somebody could help me here? Problem: ID cannot be set, while creating or updating a record. We recieve vacancies in a csv format, which we import. But the ID changes every time because it increments at each import and it should actually be the customer_id. Vacancy.create(:id => 10200, :first_name => "John", :last_name => "Doe") doesn''t work Does anybody know, how I could get inserted the records with their foreign ID from the csv export? Thanks a lot in advance for your help Rafael --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
liquidautumn
2007-Jun-14 12:10 UTC
Re: ActiveRecord :: How To create a Record with a specific ID
Add customer_id to Vacancy and do Vacancy.create(:customer_id => 10200, :first_name => "John", :last_name => "Doe"). This raises the question where Customer with corresponding id will come from, but it''s up to you how to solve this problem. Don''t mess with primary key, anyways. Rafael wrote:> Hi folx, > > may somebody could help me here? > > Problem: > > ID cannot be set, while creating or updating a record. > We recieve vacancies in a csv format, which we import. But the ID > changes every time because it increments at each import and it should > actually be the customer_id. > > Vacancy.create(:id => 10200, :first_name => "John", :last_name => > "Doe") > > doesn''t work > > Does anybody know, how I could get inserted the records with their > foreign ID from the csv export? > > Thanks a lot in advance for your help > > Rafael--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rafael
2007-Jun-14 12:18 UTC
Re: ActiveRecord :: How To create a Record with a specific ID
Thats what I have already, but what I don''t want. On every import cycle, the show id changes for the same vacancy. so that knowbody can send a link to anybody, because after the import, the id wont fit any more. somebody else has a clue? On 14 Jun., 14:10, liquidautumn <d...-PfseE6/LE5QxpvK4C0GaPQ@public.gmane.org> wrote:> Add customer_id to Vacancy and do Vacancy.create(:customer_id => > 10200, :first_name => "John", :last_name => "Doe"). This raises the > question where Customer with corresponding id will come from, but it''s > up to you how to solve this problem. Don''t mess with primary key, > anyways. > > Rafael wrote: > > Hi folx, > > > may somebody could help me here? > > > Problem: > > > ID cannot be set, while creating or updating a record. > > We recieve vacancies in a csv format, which we import. But the ID > > changes every time because it increments at each import and it should > > actually be the customer_id. > > > Vacancy.create(:id => 10200, :first_name => "John", :last_name => > > "Doe") > > > doesn''t work > > > Does anybody know, how I could get inserted the records with their > > foreign ID from the csv export? > > > Thanks a lot in advance for your help > > > Rafael--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Roland Swingler
2007-Jun-14 12:29 UTC
Re: ActiveRecord :: How To create a Record with a specific ID
I don''t know if this is really the "best practice" way of doing this, but this should work: vacancy = Vacancy.new(:first_name => ''John'', :etc => ''blah'') do |v| v.id = 10200 end vacancy.save! You will presumably get an exception if you try and save something with the same id, so you will need to check for it first (and probably wrap the whole thing up in a transaction unless you know you''re the only one doing inserts). Cheers, Roland On Jun 14, 1:03 pm, Rafael <beyoume...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi folx, > > may somebody could help me here? > > Problem: > > ID cannot be set, while creating or updating a record. > We recieve vacancies in a csv format, which we import. But the ID > changes every time because it increments at each import and it should > actually be the customer_id. > > Vacancy.create(:id => 10200, :first_name => "John", :last_name => > "Doe") > > doesn''t work > > Does anybody know, how I could get inserted the records with their > foreign ID from the csv export? > > Thanks a lot in advance for your help > > Rafael--~--~---------~--~----~------------~-------~--~----~ 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
2007-Jun-14 12:35 UTC
Re: ActiveRecord :: How To create a Record with a specific ID
On Jun 14, 2007, at 8:10 AM, liquidautumn wrote:> Add customer_id to Vacancy and do Vacancy.create(:customer_id => > 10200, :first_name => "John", :last_name => "Doe"). This raises the > question where Customer with corresponding id will come from, but it''s > up to you how to solve this problem. Don''t mess with primary key, > anyways. > Rafael wrote: >> Hi folx, >> >> may somebody could help me here? >> >> Problem: >> >> ID cannot be set, while creating or updating a record. >> We recieve vacancies in a csv format, which we import. But the ID >> changes every time because it increments at each import and it should >> actually be the customer_id. >> >> Vacancy.create(:id => 10200, :first_name => "John", :last_name => >> "Doe") >> >> doesn''t work >> >> Does anybody know, how I could get inserted the records with their >> foreign ID from the csv export? >> >> Thanks a lot in advance for your help >> >> RafaelI think you should seriously consider what liquidautumn says about the primary key (do you actually have a Customer model?), but what you want can be done. Vacancy.new(:first_name => "John", :last_name => "Doe") do |v| v.id = 10200 v.save end Now that you have this power, please use it wisely. -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 -~----------~----~----~----~------~----~------~--~---
Benjamin Curtis
2007-Jun-14 12:53 UTC
Re: ActiveRecord :: How To create a Record with a specific ID
You can just skip the helpers and interact more closely with the DB. You''ll want to do this for performance reasons as well, if you are importing large numbers of records. Vacancy.connection.insert(''insert into vacancies (id, first_name, last_name) values (10200, "John", "Doe")'') -- Benjamin Curtis http://www.bencurtis.com/ -- blog http://agilewebdevelopment.com/rails-ecommerce -- build e-commerce sites with Rails On Jun 14, 2007, at 5:03 AM, Rafael wrote:> > Hi folx, > > may somebody could help me here? > > Problem: > > ID cannot be set, while creating or updating a record. > We recieve vacancies in a csv format, which we import. But the ID > changes every time because it increments at each import and it should > actually be the customer_id. > > Vacancy.create(:id => 10200, :first_name => "John", :last_name => > "Doe") > > doesn''t work > > Does anybody know, how I could get inserted the records with their > foreign ID from the csv export? > > Thanks a lot in advance for your help > > Rafael > > > >--~--~---------~--~----~------------~-------~--~----~ 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
2007-Jun-14 13:17 UTC
Re: ActiveRecord :: How To create a Record with a specific ID
On Jun 14, 2007, at 8:35 AM, Rob Biedenharn wrote:> On Jun 14, 2007, at 8:10 AM, liquidautumn wrote: >> Add customer_id to Vacancy and do Vacancy.create(:customer_id => >> 10200, :first_name => "John", :last_name => "Doe"). This raises the >> question where Customer with corresponding id will come from, but >> it''s >> up to you how to solve this problem. Don''t mess with primary key, >> anyways. >> Rafael wrote: >>> Hi folx, >>> >>> may somebody could help me here? >>> >>> Problem: >>> >>> ID cannot be set, while creating or updating a record. >>> We recieve vacancies in a csv format, which we import. But the ID >>> changes every time because it increments at each import and it >>> should >>> actually be the customer_id. >>> >>> Vacancy.create(:id => 10200, :first_name => "John", :last_name => >>> "Doe") >>> >>> doesn''t work >>> >>> Does anybody know, how I could get inserted the records with their >>> foreign ID from the csv export? >>> >>> Thanks a lot in advance for your help >>> >>> Rafael > > I think you should seriously consider what liquidautumn says about > the primary key (do you actually have a Customer model?), but what > you want can be done. > > Vacancy.new(:first_name => "John", :last_name => "Doe") do |v| > v.id = 10200 > v.save > end > > Now that you have this power, please use it wisely. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgOn Jun 14, 2007, at 8:18 AM, Rafael wrote:> > Thats what I have already, but what I don''t want. > On every import cycle, the show id changes for the same vacancy. > > so that knowbody can send a link to anybody, because after the import, > the id wont fit any more. > > somebody else has a clue?On Jun 14, 2007, at 8:53 AM, Benjamin Curtis wrote:> You can just skip the helpers and interact more closely with the DB. > You''ll want to do this for performance reasons as well, if you are > importing large numbers of records. > > Vacancy.connection.insert(''insert into vacancies (id, first_name, > last_name) values (10200, "John", "Doe")'') > > -- > Benjamin Curtis > http://www.bencurtis.com/ -- blog > http://agilewebdevelopment.com/rails-ecommerce -- build e-commerce > sites with RailsAh, perhaps you need something like: vacancy = Vacancy.find_or_initialize_by_id_and_first_name_and_last_name(10200, "John", "Doe") if vacancy.new_record? vacancy.id = 10200 vacancy.save end But you might get better performance using Benjamin''s suggestion if you just need the "insert only if it''s not there" behavior. However, if this CSV file can change the name on a specific vacancy: if vacancy = Vacancy.find_by_id(10200) vacancy.update_attributes(:first_name => "John", :last_name => "Doe") else vacancy = Vacancy.new(:first_name => "John", :last_name => "Doe") do |v| v.id = 10200 v.save end end -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 -~----------~----~----~----~------~----~------~--~---
MichaelLatta
2007-Jun-14 15:18 UTC
Re: ActiveRecord :: How To create a Record with a specific ID
If the value changes it is not the primary key. Just let the system create the primary key for you. If you need to be able to replace records with new ones just delete the old ones before the import. If you are updating entries then find the correct record based on the stable data and then update from the imported row. Michael On Jun 14, 5:03 am, Rafael <beyoume...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi folx, > > may somebody could help me here? > > Problem: > > ID cannot be set, while creating or updating a record. > We recieve vacancies in a csv format, which we import. But the ID > changes every time because it increments at each import and it should > actually be the customer_id. > > Vacancy.create(:id => 10200, :first_name => "John", :last_name => > "Doe") > > doesn''t work > > Does anybody know, how I could get inserted the records with their > foreign ID from the csv export? > > Thanks a lot in advance for your help > > Rafael--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rafael
2007-Jun-17 15:06 UTC
Re: ActiveRecord :: How To create a Record with a specific ID
Hi everybody, first thank you all for your help! This helps a lot. But actually not in this project anymore :-( The client changed their Export and their unique key is now something like this 130003-31. because it isn''t an integer anymore I had to find another solution: I go for is a REST Solution now. vacancies/130003-31 this show''s me always the right ID, because I changed the select to REST URL vacancy_path(vacancy.vacancy_number) show action find_by_vacancy_number(params[:id]) This takes me to another question, but this I will try to post in another topic. Thanks anyway! I tried the "connection" solution, and it is the best solution except, that their is a quoting problem with special chars. Greetinx Rafael On 14 Jun., 17:18, MichaelLatta <lat...-ee4meeAH724@public.gmane.org> wrote:> If the value changes it is not the primary key. Just let the system > create the primary key for you. > > If you need to be able to replace records with new ones just delete > the old ones before the import. If you are updating entries then find > the correct record based on the stable data and then update from the > imported row. > > Michael > > On Jun 14, 5:03 am, Rafael <beyoume...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi folx, > > > may somebody could help me here? > > > Problem: > > > ID cannot be set, while creating or updating a record. > > We recieve vacancies in a csv format, which we import. But the ID > > changes every time because it increments at each import and it should > > actually be the customer_id. > > > Vacancy.create(:id => 10200, :first_name => "John", :last_name => > > "Doe") > > > doesn''t work > > > Does anybody know, how I could get inserted the records with their > > foreign ID from the csv export? > > > Thanks a lot in advance for your help > > > Rafael--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---