Default Scoping
1518 ワード
class Article < ActiveRecord::Base
default_scope :order => 'created_at DESC'
end
いずれかのfindまたはnamed_scopeメソッドが実行され、結果リストはcreated_に従います.at DESCを並べ替えた.
Article.find(:all) #=> "SELECT * FROM `articles` ORDER BY created_at DESC"
class Article < ActiveRecord::Base
default_scope :order => 'created_at DESC'
named_scope :published, :conditions => { :published => true }
end
Article.published #=> "SELECT * FROM `articles` WHERE published = true ORDER BY created_at DESC"
class Article < ActiveRecord::Base
default_scope :order => 'created_at DESC'
named_scope :published, :conditions => { :published => true },
:order => 'published_at DESC'
end
# published_at DESC clobbers default scope
Article.published
#=> "SELECT * FROM `articles` WHERE published = true ORDER BY published_at DESC"
class Article < ActiveRecord::Base
default_scope :order => 'created_at DESC'
end
# Ignore other scoping within this block
Article.with_exclusive_scope { find(:all) } #=> "SELECT * FROM `articles`