我在Rails 3.2应用程序中。我希望当管理用户通过CRUD (通过标准Rails支架方式对update_attributes调用进行更改)时,将名为候选人的特定字段保存到更新模型中。
我在一个跟踪before_save过滤器中的变化的候选模型中有代码:
before_save :check_changed
def check_changed
period_contributions_changed?
end
这是在candidate.rb中,候选模型文件。
我想要的是period_contributions和其他指定字段中更改的值,以触发另一个记录更新的创建。
更新将包含候选ID、更改字段的名称、前一个值和新值。
这似乎需要在Update控制器上调用一个方法的模型代码,这似乎是错误的做法。这样做有什么最佳做法吗?
发布于 2014-01-17 14:22:09
ActiveRecord
提供了一些可以使用的方法。
changed?
->告诉您对象已被更改。changed
->提供了一个更改的属性数组。changed_attributes
->为您提供了一个哈希,其中键作为已更改的属性值,值作为旧值。
before_save :log_changed def log_changed if self.changed?self.changed.each do x attr\ Update.create(:candidate_id => self.id,:field => attr,:new_value => self.attributesattr,:old_value => self.changed_attributesattr)末端https://stackoverflow.com/questions/21195975
复制相似问题