Hey everyone, I''m interested in extending ActiveRecord::Base. I have a special "easier" find function that I want to include in all classes. How can I include this in all classes? Can I just create a model object that does something like this: class ExtendedAR < ActiveRecord::Base def self.special_find ... end end and then all my other classes inherit from this class? That way, they inherit the functionality? Thanks in advance, as always, Julian.
On Sunday 11 September 2005 15:11, Julian Leviston wrote:> I''m interested in extending ActiveRecord::Base. I have a special > "easier" find function that I want to include in all classes.Here''s how I do it module MyActiveRecordExtensions def self.included(base) base.class_eval do base.extend(ClassMethods) end end module ClassMethods def special_find ... end end end Then wire things together somewhere ActiveRecord::Base.class_eval do include MyActiveRecordExtensions end Michael -- Michael Schuerig Those people who smile a lot mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Watch the eyes http://www.schuerig.de/michael/ --Ani DiFranco, Outta Me, Onto You
Where do you put all that? It doesn''t really make sense to me. Probably because I don''t know about modules yet. I''ll just look at it a bit longer tho. Julian. On 12/09/2005, at 12:34 AM, Michael Schuerig wrote:> On Sunday 11 September 2005 15:11, Julian Leviston wrote: > > >> I''m interested in extending ActiveRecord::Base. I have a special >> "easier" find function that I want to include in all classes. >> > > Here''s how I do it > > module MyActiveRecordExtensions > def self.included(base) > base.class_eval do > base.extend(ClassMethods) > end > end > > module ClassMethods > def special_find > ... > end > end > end > > Then wire things together somewhere > > ActiveRecord::Base.class_eval do > include MyActiveRecordExtensions > end > > > Michael > > -- > Michael Schuerig Those people who smile a lot > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Watch the eyes > http://www.schuerig.de/michael/ --Ani DiFranco, Outta Me, Onto You > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Monday 12 September 2005 04:09, Julian Leviston wrote:> Where do you put all that? > > It doesn''t really make sense to me. Probably because I don''t know > about modules yet. I''ll just look at it a bit longer tho.See below> > Here''s how I do it > > > > module MyActiveRecordExtensions > > def self.included(base) > > base.class_eval do > > base.extend(ClassMethods) > > end > > end > > > > module ClassMethods > > def special_find > > ... > > end > > end > > endlib/my_active_record_extensions.rb would be a suitable place> > Then wire things together somewhere > > > > ActiveRecord::Base.class_eval do > > include MyActiveRecordExtensions > > endThis could go into config/environment.rb or into the same file as the above. Michael -- Michael Schuerig Life is what happens mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org While you''re making plans http://www.schuerig.de/michael/ --Kevin Gilbert, A Long Day''s Life