Craig Demyanovich
2008-Oct-22 13:35 UTC
Re: Returning a record''s ID before the record is made?!??
In their example, def create @product = Product.new(params[:product]) @details = Detail.new(params[:details]) Product.transaction do @product.save! @details.product = @product @details.save! redirect_to :action => :show, :id => @product end rescue ActiveRecord::RecordInvalid => e @details.valid? # force checking of errors even if products failed render :action => :new end they assign the product to the details, @details.product = @product. When you do that with related ActiveRecord objects, ActiveRecord takes care of setting the IDs for you. In this case, @details.product_id will be set to @ product.id. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michael Kahle
2008-Oct-22 22:37 UTC
Re: Returning a record''s ID before the record is made?!??
Craig Demyanovich wrote:> In their example, > ... [snip] > they assign the product to the details, @details.product = @product. > When > you do that with related ActiveRecord objects, ActiveRecord takes care > of > setting the IDs for you. In this case, @details.product_id will be set > to @ > product.id. > > Regards, > CraigThanks for your post Craig. I could not wrap my head around this concept, partially because it was so difficult for me to implement in JSP that I resorted to using randomly generated UUID''s before inputing the record into the database. Active Record to the rescue indeed. Wow. I spent about an hour playing with this last night and was able to magically get this to, well, just work in Rails. When I wrote this post originally I hadn''t yet tried the example as I just couldn''t understand the code and how it worked. I took the leap of faith and followed the example and Ta-da, onto more interesting problems. I should also not that another user wrote me off-list and I''d like to thank him/her for the wonderfully helpful response. I am not mentioning his/her name in the event that they wrote off list because they wanted to remain anonymous. But thank you. They suggested I read: http://railsguides.phusion.nl/activerecord/association_basics.html and it helped immensely. I felt like I missed a meeting! Ruby/Rails has been such a fun experiment thus far. I''m really loving it''s elegance and simplicity. Thanks again to all. TTFN. -- 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 -~----------~----~----~----~------~----~------~--~---