I do this by putting this code in lib/has_common_named_scopes.rb:
module HasCommonNamedScopes
def self.included(base)
base.class_eval {
named_scope :newest, :order => "#{base.table_name}.created_at DESC"
named_scope :freshest, :order => "#{base.table_name}.updated_at DESC"
named_scope :limit, lambda { |limit| {:limit => limit} }
}
end
end
and then include the module in each model where I need them:
class MyModel < ActiveRecord::Base
include HasCommonNamedScopes
I would recommend using base.table_nametables to classify columns when referenced in these named areas, as shown in the example. Otherwise, you will encounter problems with ambiguous references when combining these named areas with other areas that are combined into other tables.
Update:
scope Rails > 3 named_scope .