How to get changes of transient attributes in models tracked by ActiveRecords?
Assuming a model with a non transient attribute a and a transient attribute b:
class Model < ActiveRecord::Base
attr_accessor :b
end
I want to get Rails to track attribute changes of both attributes. Currently, model.changes does only track changes of attribute a.
What I have tried so far:
Use ActiveModel::Dirty methods:
include ActiveRecord::AttributeMet开发者_JS百科hods::Dirty
class Model < ActiveRecord::Base
attr_accessor :b
define_attribute_methods [:b]
def b
@b
end
def b=(val)
b_will_change! unless val == @b
@b = val
end
end
Unfortunately Rails doesn't like it and throws a TypeError.
Are there any ideas how to get this done?
By definition, a non-persisted variable is either nil or dirty.
精彩评论