This is the update action in notes_controller.rb.
def update
note = current_user.notes.find(params[:id])
note.update_attributes! params[:note]
render json: {note: note.display}
end
It works fine.
My note object has a datetime field called scheduled_time. When the JSON data is returned from the update action on the client side, I need to know if the date that the note’s updated scheduled_time falls on is the same as it was before the update.
def update
note = current_user.notes.find(params[:id])
previous_date = note.scheduled_time.to_date
note.update_attributes! params[:note]
date_changed = previous_date != note.scheduled_time.to_date
render json: {note: note.display, date_changed: date_changed}
end
But adding this code causes the note returned in the JSON data (note.display) to contain the values from before the update_attributes!. The update_attributes! still works, but the JSON data returned from the update action does not reflect the update. Why would this be happening?
I’m using
Rails 3.2.0
ruby 1.9.3
I’m also using the Memoist gem generally in the note model, but not on the display method specifically.
The issue seems to be the assigning of the note variable and it not being updated with the result set of #update_attributes!. Do you have any hooks or observers going on?
Try using the Note object directly:
or if all else fails try:
It seems to have something to do with the Memoist gem you’re using but I can’t be sure without diving into and simulating this.