So, i have form that updates a record in the database. In my controllers update action i want to change a value to something if another one of the values is Estimate. Maybe this will make more sense…this is what i’m trying to do.
def update
@invoice = Invoice.find(params[:id])
if @invoice.update_attributes(params[:invoice])
if@invoice.status == "Estimate"
# if the value of status is Estimate then change the
# value of estimate_sent_date to the current timestamp
end
redirect_to invoices_path
else
render 'edit'
end
end
The only values of the form that i’m concerned with are status and estimate_sent_date. Mostly, i’m just not sure how to change the value of estimate_sent_date and save that record.
Also, should i save everything and then do a separate call to save the estimate_sent_date or just save it all at once? I guess i could change the value of estimate_sent_date before calling if @invoice.update_attributes(params[:invoice]), couldn’t i?
Thanks for the help!
As Ryan Bigg says, state machine does work here. An alternative solution is to use a
before_savecallback on the Invoice model, like so: