use*_*052 4 ruby activerecord compare ruby-on-rails ruby-on-rails-3
我正在使用Ruby on Rails 3,我有两个同一类Account的ActiveRecord对象,如下所示:
# Account1
<Account id: 1, name: "Test name 1", surname: "Test surname 1", email: "...", ...>
和
# Account2
<Account id: 2, name: "Test name 2", surname: "Test surname 2", email: "...", ...>
如何在几行代码中比较Account1的每个属性和Account2的相应属性来测试值是否相等?如果Account1的所有值都等于Account2的值,我应该收到'true'的输出,否则'false'即使只有一个是不同的.
noo*_*odl 18
account1.attributes == account2.attributes
那里,这很短.请注意,id包含在这些属性中.您可以在两者上使用.clone来避免这种情况,或者以其他方式将其从属性哈希中排除.例如:
account1.attributes.except('id') == account2.attributes.except('id')
| 归档时间: | 
 | 
| 查看次数: | 7412 次 | 
| 最近记录: |