I am new to Ruby, so apologies. I'm attempting to combine the input of two objects into one form. The objects are customer and registration. What concerns me is how will I be able to return the record ID of the customer object to the registration object so that when the registration object is saved it contains that customer ID? I am learning via the book Agile Web D. with Rails (Third Edition) and in the section on Action view it describes in a section called "Multiple Models in a Form" a technique used to do this in a form using the fields_for method. Then in the registration controller I would specify the creation of a new customer object in the new action. I would also create in the create action of this controller a transaction to ensure that (from the book) 1. If either model contains invalid data, neither model should be saved. 2. If both models contain validation errors, we want to display the messages from both... I couldn't agree more. However in the registration object there is a field called customer_id that has to be populated with the record_id from the customer model. How does one get around this? Thanks for your thoughts in advance.
on 2008-10-22 09:22
on 2008-10-22 17:35
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
on 2008-10-23 02:37
Craig D. 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, > Craig Thanks 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/associa... 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.
on 2008-10-23 16:28
You shouldn't have to worry about that. In this code: Product.transaction do @product.save! #<-- this saves the product, retrieving the db-assigned ID @details.product = @product #<-- this writes @product.id to @details.product_id @details.save! #<-- this saves the @details (since it's associated w/the product) redirect_to :action => :show, :id => @product end HTH -Roy
on 2008-12-10 12:13
On Oct 23, 5:26 pm, "Pardee, Roy" <firstname.lastname@example.org> wrote: > You shouldn't have to worry about that. In this code: > > Product.transaction do > @product.save! #<-- this saves the product, retrieving the db-assigned ID > @details.product = @product #<-- this writes @product.id to @details.product_id > @details.save! #<-- this saves the @details (since it's associated w/the product) > redirect_to :action => :show, :id => @product > end > Awesome! Thanks a lot!