【发布时间】:2012-11-23 10:10:56
【问题描述】:
我的before_update 方法仅在特定字段发生更改时才需要更新另一个对象。我们可以访问原始数据还是需要从数据库中加载它?例如:
class Log < ActiveRecord::Base
attr_accessible :points, :student_id
belongs_to :student
before_update :update_points
private
def update_points
if points != original_log.points
student.points += points - original_log.points
student.save
end
end
end
我需要那个original_log 或原来的points。如果我无权访问数据库,我认为在 def update_points 下添加它是安全的?
original_log = Log.find(id)
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord orm ruby-on-rails-3.2