I am building a Rails application and I am in front of an issue with form I can't fix.
I have an order
model which belongs_to customer
so when I build the form I do @order = @customer.orders.build
This works well to save orders attributes but I also want to update customer attributes as well which never work.
How can I save order and update "parent" customer attributes in the same process ?
Thanks for your help !
Edit:
Customer model:
class Customer < ActiveRecord::Base
has_many :orders
end
Order model:
class Order < ActiveRecord::Base
belongs_to :customer
accepts_nested_attributes_for :customer
end
My project:
You should update your customer in your create method. How about:
def create
@order = current_customer.orders.build order_params
if @order.save
@order.customer.update_attributes(order_params[:customer_attributes])
...
else
...
end
end