Reputation: 54949
I am preparing the Object variables before i start saving the details into the Database.
I have 3 objects i need to save order
, order_event
, message
I have instanciated the object order_event and want to update the order parameters before i save to the database
@order_event = OrderEvent.new(order_event_params)
@order_event.update_attributes(event_date: @event.event_date)
But Rails is trying to save it to the Database and throwing the following error
Mysql2::Error: Field 'order_id' doesn't have a default value: INSERT INTO `order_events` (`created_at`, `event_date`, `event_id`, `no_of_ppl`, `updated_at`) VALUES ('2015-04-17 10:20:11', '2015-01-22 00:00:00', 2, 1, '2015-04-17 10:20:11')
My Model
--- !ruby/object:OrderEvent
attributes:
id:
order_id:
event_id: 2
no_of_ppl: 3
event_date:
cost:
total_cost:
status:
created_at:
updated_at:
I want to update event_date, cost, total_cost etc before saving.
Upvotes: 0
Views: 37
Reputation: 5693
You can assign the value to your params hash and then update:
@order_event = OrderEvent.find(params[:id])
params[:order_event][:event_date] = @event.event_date
if @order_event.update_attributes(order_event_params)
...
Upvotes: 1
Reputation: 1596
Use assign_attributes
for assigning params instead of update_attributes
(it persists them)
Upvotes: 1