本文介绍了Rails 3 检查属性是否更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 3 中更新之前需要检查属性块是否已更改.

Need to check if a block of attributes has changed before update in Rails 3.

street1、street2、城市、州、邮政编码

street1, street2, city, state, zipcode

我知道我可以使用类似的东西

I know I could use something like

if @user.street1 != params[:user][:street1]
  then do something....
end

但是那段代码真的很长.有没有更干净的方法?

But that piece of code will be REALLY long. Is there a cleaner way?

推荐答案

查看 ActiveModel::Dirty(默认适用于所有型号).该文档非常好,但它可以让您执行以下操作:

Check out ActiveModel::Dirty (available on all models by default). The documentation is really good, but it lets you do things such as:

@user.street1_changed? # => true/false

这篇关于Rails 3 检查属性是否更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 23:36