开发者

Rails update_attribute did not hit the mysql database

开发者 https://www.devze.com 2022-12-08 09:56 出处:网络
It only happens in production, when we update some of the records through browser, the change was not saved. it does not seem to be a开发者_如何学C cache problem as we verified that the data in mysql

It only happens in production, when we update some of the records through browser, the change was not saved. it does not seem to be a开发者_如何学C cache problem as we verified that the data in mysql was still the old data. However, the controller did get hit and flash message returned as if the change was made successfully.

However, we can make the change manually in rails console or mysql withhout any problem.

Any ideas why this is happening?

btw, we recently reconfigure the site to use SSL, it might have something to do with that.


Is there anything that could've prevented the model from being saved?

One way to ensure that the attributes are set and the model is saved is to use the exception raising version which can help fix problems like this:

def update
  @model = Model.find(params[:id])

  @model.update_attributes(params[:model])

  redirect_to(model_path(@model))
end

This could be improved to a more reliable method:

def update
  @model = Model.find(params[:id])

  # Use exception-throwing update_attributes!
  @model.update_attributes!(params[:model])

  redirect_to(model_path(@model))

rescue ActiveRecord::RecordNotFound
  render(:partial => 'not_found')
rescue ActiveRecord::RecordInvalid
  # Delegate back to edit action, something's invalid
  edit

  render(:action => 'edit')
end

There are occasions where update_attributes may not successfully save.

If you can perform the same update on the same data with the same methods then that is peculiar.

0

精彩评论

暂无评论...
验证码 换一张
取 消