Hey all im trying to do somthing like this... if request.post? @customer = CustomerAnonymous.new(params[:customer]) @customer.customer_addresses.new(params[:delivery]) end However, the validation on my CustomerAnonymous model is being invoked no problem, but my CustomerAddress model validation isnt, any ideas or work arounds for this? Is this even the best way to be inserting data into multiple tables from a single form? Cheers Tim -- 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 -~----------~----~----~----~------~----~------~--~---
Tim Perrett wrote:> Hey all > > im trying to do somthing like this... > > if request.post? > @customer = CustomerAnonymous.new(params[:customer]) > @customer.customer_addresses.new(params[:delivery]) > end > > However, the validation on my CustomerAnonymous model is being invoked > no problem, but my CustomerAddress model validation isnt, any ideas or > work arounds for this? > > Is this even the best way to be inserting data into multiple tables from > a single form? > > Cheers > > TimTim, I''m assuming that you''ve got a model called CustomerAddress and that a CustomerAnonymous has_one CustomerAddress, in which case I think you need something like this (but I''m learning too...) if request.post? @customer = CustomerAnonymous.new(params[:customer]) @customer.customer_addresses = CustomerAddress.new(params[:delivery]) # Assuming that @customer.save is coming here or soon after... end I''m not sure if this is still the case, but the first edition of the rails book says that the CustomerAddress save will fail silently if anything goes wrong, so it recommends... if request.post? address = CustomerAddress.new(params[:delivery]) unless address.save! # <-- Throws an exception if save fails. @customer = CustomerAnonymous.new(params[:customer]) @customer.customer_addresses = address # Assuming that @customer.save is coming here or soon after... end Hope that helps. -- 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 -~----------~----~----~----~------~----~------~--~---
Excellent, i have that first edition of the rails book but i couldnt find i page on that for love nor money!! Ill give that a whirl now! Cheers Tim -- 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 -~----------~----~----~----~------~----~------~--~---
Tim Perrett wrote:> Excellent, i have that first edition of the rails book but i couldnt > find i page on that for love nor money!!It''s in the section that describes one-to-one associations in the active-record basics chapter, I don''t have the book with me to give you the page number unfortunately. -- 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 -~----------~----~----~----~------~----~------~--~---
Ok cool, i am getting somwhere now, however i think there is a problem witht he fact that one customer could have many addresses... im getting the error... undefined method `each'' for #<CustomerAddress:0x226cde0> out of activerecord-1.14.2/lib/active_record/base.rb:1792:in `method_missing'' activerecord-1.14.2/lib/active_record/associations/association_collection.rb:118:in `replace'' activerecord-1.14.2/lib/active_record/associations.rb:895:in `customer_addresses='' #{RAILS_ROOT}/app/controllers/store/checkout_controller.rb:34:in `anonymous_checkout'' Any suggestions? as one customer could have many different address on my system. My code currently looks like if request.post? address = CustomerAddress.new(params[:delivery]) if address.save! # <-- Throws an exception if save fails. @customer = Customer.new(params[:customer]) @customer.customer_addresses = address begin @customer.save! rescue => @error flash[:notice] = ''Could not be saved.'' end else flash[:notice] = ''address not valid in model'' end end Cheers Tim -- 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 -~----------~----~----~----~------~----~------~--~---
Interstingly... if request.post? address = CustomerAddress.new(params[:delivery]) unless address.save! # <-- Record sucseeds validation, and insertes a record in DB @customer = Customer.new(params[:customer]) @customer.customer_addresses = address # <-- this is the problem line where the ''each'' exception is comming from. begin @customer.save! rescue => @error flash[:notice] = ''Could not be saved.'' end else flash[:notice] = ''address not valid in model'' end end -- 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 -~----------~----~----~----~------~----~------~--~---
Tim Perrett <rails-mailing-list@...> writes:> > > Interstingly... > > if request.post? > address = CustomerAddress.new(params[:delivery]) > unless address.save! # <-- Record sucseeds validation, and > insertes a record in DB > <at> customer = Customer.new(params[:customer]) > <at> customer.customer_addresses = address # <-- this is the > problem line where the ''each'' exception is comming from. > begin > <at> customer.save! > rescue => <at> error > flash[:notice] = ''Could not be saved.'' > end > else > flash[:notice] = ''address not valid in model'' > end > end >for a has_one: customer.customer_address = address for a has_many: customer.customer_addresses << address where << is usually used to append something (address) to something else (customer.customer_addresses) Gareth --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Excellent, i had manged to work out that it was using an array type thing, as customer.customer_addresses[0] also inserted it in the correct place, but the << solution is a far slicker example! Many thanks for all you replys, this is what i ended up doing, well at least its a start, i can add more error checking and presentaion from here! Many thanks again! Tim if request.post? @customer = CustomerAnonymous.new(params[:customer]) @customer.customer_addresses << CustomerAddress.new(params[:delivery]) begin if @customer.save! begin @order = Order.update(session[:order_id], :customer_id => @customer.id) rescue => @error flash[:notice] = ''Your order could not be updated.'' end end rescue => @error flash[:notice] = ''Please ensure you have filled in all the required feilds ('' + @error + '')'' end end -- 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 -~----------~----~----~----~------~----~------~--~---