Rails & ActiveRecord: Appending methods to models that inherit from ActiveRecord::Base

Posted by PlankTon on Stack Overflow See other posts from Stack Overflow or by PlankTon
Published on 2012-03-31T11:24:17Z Indexed on 2012/03/31 11:29 UTC
Read the original article Hit count: 502

Filed under:
|

I have a standard ActiveRecord model with the following:

class MyModel < ActiveRecord::Base
  custom_method :first_field, :second_field
end

At the moment, that custom_method is picked up by a module sent to ActiveRecord::Base. The functionality basically works, but of course, it attaches itself to every model class, not just MyModel. So if I have MyModel and MyOtherModel in the same action, it'll assume MyOtherModel has custom_method :first_field, :second_field as well.

So, my question is: How do I attach a method (eg: def custom_method(*args)) to every class that inherits from ActiveRecord::Base, but not by attaching it to ActiveRecord::Base itself?

Any ideas appreciated.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby