Here is the code for update in invoice controller:
def update
@invoice = Invoice.find(params[:id])
if @invoice.approved_by_corp_head_changed?
@invoice.approve_date_corp_head = Time.now
@invoice.corp_head_id = session[:user_id]
end
if @invoice.update_attributes(params[:invoice], :as => :roles_update)
redirect_to URI.escape("/view_handler?index=0&msg=Updated")
else
flash.now[:error] = 'Not saved!'
render 'edit'
end
end
Value of approved_by_corp_head was saved correctly. The problem is that if @invoice.approved_by_corp_head_changed? was always false and the IF loop never gets executed. In rails console, the @invoice.approved_by_corp_head_changed? is true if the value is changed. What could be wrong here? Thanks so much.
Because you fetch an object directly from the database, it’s brand new, so there is no reason it could be changed somehow.
Moreover,
_changed?is available until the object is actually saved.One option would be to get the changes from the
paramsbut not update your object directly. This issue has been discussed here.Sidenote: why do you do:
instead of: