覆盖模型getter在保存时执行?

时间:2014-01-24 17:04:12

标签: ruby-on-rails ruby activerecord callback getter

我正在修复Rails插件中的错误,我刚刚找到导致问题的方法。我的猜测是它与某个getter方法在其ActiveRecord类Sprint中被覆盖有关。方法名称为burndown,还有一个名为burndown的属性,用于存储序列化哈希。

class Sprint < ActiveRecord::Base

  serialize :burndown, Hash
  ...

  def touch!
    ... do stuff ...
    self.burndown = nil
    self.save!
  end

  def burndown
    ... some crazy-ass method ...
  end
end

所以burndown方法以某种方式在保存时执行,但我不确定为什么,因为没有为Sprint类定义回调。覆盖getter方法是否可能导致这种情况?

1 个答案:

答案 0 :(得分:0)

即使您没有任何验证或回调,也会在保存时调用Getter方法。

"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.13/lib/active_model/dirty.rb:143:in `attribute_change'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.13/lib/active_model/dirty.rb:117:in `block in changes'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.13/lib/active_model/dirty.rb:117:in `map'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.13/lib/active_model/dirty.rb:117:in `changes'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/attribute_methods/dirty.rb:23:in `save'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/transactions.rb:259:in `block (2 levels) in save'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/transactions.rb:208:in `transaction'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/transactions.rb:259:in `block in save'"
"/home/user/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/transactions.rb:270:in `rollback_active_record_state!'"
"/home/usha/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.13/lib/active_record/transactions.rb:258:in `save'"

因此,如果您覆盖属性的getter,请确保它仍然执行其原始函数