Question

I'm currently working on fixing a bug in Rails plugin and I just found the method which causes the issue. My guess is that it's somehow related to one of the getter methods being overwritten in its ActiveRecord class Sprint. The method name is burndown and there is also an attribute called burndown which stores serialized Hash.

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

So the burndown method somehow gets executed on save but I'm not really sure why, as there are no callbacks defined for the Sprint class. Is it possible that overwriting the getter method causes that?

Was it helpful?

Solution

Getter method gets called on save even if you don't have any validations or callback.

"/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'"

So if you override getter for an attribute, make sure that it still performs its original function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top