开发者

How to get changes of transient attributes in models tracked by ActiveRecords?

开发者 https://www.devze.com 2023-03-13 22:41 出处:网络
Assuming a model with a non transient attribute a and a transient attribute b: class Model < ActiveRecord::Base

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消