# Model Callback.rb insidiousally breaks other models

**URL:** <https://www.ruby-forum.com/t/model-callback-rb-insidiousally-breaks-other-models/119634>\
**Category:** Rails\
**Created:** [November 8, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/model-callback-rb-insidiousally-breaks-other-models/119634 "2007-11-08T19:37:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [November 8, 2007, 7:37pm UTC](https://www.ruby-forum.com/t/model-callback-rb-insidiousally-breaks-other-models/119634/1 "2007-11-08T19:37:25Z")

</div>

I’ve run into an odd case where a model called “Callback” breaks  
ActiveRecord::Callbacks, for models other then itself.

It’s understandable if Callback is a model name I shouldn’t have used,  
but its rather insidious in that it works just fine on its own, but  
breaks when “belongs\_to” from other models includes it.

## The following is how to duplicate this, and the results I’m getting.

echo “create database a\_development” | mysql -u root ; rails a;cd a;  
ruby script/generate model callback;ruby script/generate model  
example;rake db:migrate

Edit app/model/example.rb  
{{{  
class Example \< ActiveRecord::Base  
belongs\_to :callback # This line breaks ActiveRecord::Callbacks  
for Example.rb  
def before\_create  
puts “#{self.class.name} :: This should say something,  
but doesn’t”  
end

```
    def after_create
            puts "#{self.class.name} :: after_create says nothing"
    end

    def before_save
            puts "#{self.class.name} :: before_save says nothing"
    end
    def after_save
            puts "#{self.class.name} :: after_save says nothing"
    end
    def validate
            puts "#{self.class.name} :: validate works"
    end

```

end  
}}}  
Edit app/model/callback.rb  
{{{  
class Callback \< ActiveRecord::Base  
def before\_create  
puts "#{self.class.name} :: before\_create "  
end

```
    def after_create
            puts "#{self.class.name} :: after_create "
    end

    def before_save
            puts "#{self.class.name} :: before_save "
    end
    def after_save
            puts "#{self.class.name} :: after_save "
    end
    def validate
            puts "#{self.class.name} :: validate works"
    end

```

end  
}}}  
ruby script/console

> > e = Example.new  
> > =\> #\<Example:0x3268c04 @new\_record=true, @attributes={}\>
> 
> > e.save  
> > Example :: validate works  
> > =\> true
> 
> > c = Callback.new  
> > =\> #\<Callback:0x3210cac @new\_record=true, @attributes={}\>
> 
> > c.save  
> > Callback :: validate works  
> > Callback :: before\_save  
> > Callback :: before\_create  
> > Callback :: after\_create  
> > Callback :: after\_save  
> > =\> true
> 
> >

So it looks like Callback.rb callbacks work, but Example.rb callbacks  
don’t.  
If I comment out the “:belongs\_to :callback” then callbacks work for  
Example.rb

Not really sure what to make of this, other then don’t use any name that  
ActiveRecord makes use of.

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [November 8, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/model-callback-rb-insidiousally-breaks-other-models/119634/2 "2007-11-08T20:26:56Z")

</div>

On 8 Nov 2007, at 18:37, David H. wrote:

> So it looks like Callback.rb callbacks work, but Example.rb callbacks  
> don’t.  
> If I comment out the “:belongs\_to :callback” then callbacks work for  
> Example.rb
> 
> Not really sure what to make of this, other then don’t use any name  
> that  
> ActiveRecord makes use of.

There is a (private) method on ActiveRecord::Base called callback (in  
callbacks.rb) which is central to callbacks (callback(:foo) calls the  
callbacks defined for :foo, where :foo is something  
like :after\_validation, :before\_save etc…).  
When you say belongs\_to :callback, that defines a new method callback  
(which returns the associated object), but which overwrites the  
earlier definition, stopping callbacks from working.

Fred
