是否可以获取已经分配/更新的参数列表?
我想要的不是来自before_save
回调的已更改属性列表,而是通过任何方式分配的属性列表(直接分配然后保存或通过update_attributes
方法)。< / p>
谢谢!
更新
例如:
p = Person.new( name: 'Bob')
p.update_attributes( {:name, 'Bob'} )
以及update_attributes
之后的回调:
name_changed? # return false
我想获得一个已分配属性的列表,如:
attr_assigned # returns something like {:name => 'Bob'} or so
更新2:
这是我模特的现场会议
wl = WhiteLabel.last
wl.footer_text # "dfgdfgdfgdf"
wl.footer_text = "dfgdfgdfgdf"
wl.save
147: def on_subscription_update
148: return if !valid? || back
149:
=> 150: binding.pry
151:
152: filter_out_postponed_attributes
153: create_new_card_from_token if stripe_card_token.present?
154: update_subscription if user.stripe_customer_id && (subscription_type_id_changed? || !subscription_token)
155: session_store = nil if last_step?
156: end
changed # []
changes # {}
我也试过了wl.update_attributes({ :footer_text => "dfgdfgdfgdf"} )
。相同的结果
我是否遗漏了文档中的内容?
答案 0 :(得分:2)
ActiveModel::Dirty
模块提供的功能可让您访问已更改的属性。
record.changes # => {"name" => ["Bill", "Bob"]}
您还可以测试单个属性
record.name = 'Bob'
record.changed? # => true
record.name_changed? # => true
record.name_changed?(from: "Uncle Bob", to: "Bob") # => true
record.name_was # => "Uncle Bob"
record.name_change # => ["Uncle Bob", "Bob"]
record.name = 'Bill'
record.name_change # => ["Uncle Bob", "Bill"]
答案 1 :(得分:0)
基本上,当你执行update_attributes时,它会将您请求的更改提交到数据库中,因此更改数组变为空。如果您想在这种情况下跟踪更改,请在保存回调之前使用它,它将告诉您所有更改。