是否有相当于 AR 的命名范围?命名范围基本上是过滤器,可以包装在方法中,然后链接。
class Article < ActiveRecord::Base
# Get all articles that have been published
named_scope :published, :conditions => ['published = ?', true]
# Get all articles that were created recently
named_scope :recent, lambda { { :conditions => ['created_at >= ?', 1.week.ago] } }
end
# Get all recently created articles that have been published
Article.published.recent
这是一个使用 Django ORM 的示例:http: //furrybrains.com/2009/06/22/named-scopes-for-django/