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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Nov-08 19:26 UTC
Re: Model Callback.rb insidiousally breaks other models
On 8 Nov 2007, at 18:37, David Harkness 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---