Rails named_scope as an extension to AR :: Base

class SomeModel < ActiveRecord::Base
  named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
end

      

I want to extend AR :: Base class to have this named_scope for all models, how can I do that?

+2


a source to share


1 answer


Create a new initialization file in config / initializers and then run the ActiveRecord class again Base

to add the named scope:

module ActiveRecord
  class Base
    named_scope :recent, lambda {
      { :conditions => ['created_at > ?', 1.week.ago] }
    } 
  end
end

      



& mdash. Of course, you get a rather ugly error if you try to use this named scope on a model that has no attribute created_at

...

+3


a source







All Articles