diff options
Diffstat (limited to 'railties/guides/source')
-rw-r--r-- | railties/guides/source/active_record_querying.textile | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 6c1042e70e..5cd302f356 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -728,39 +728,39 @@ Scoping allows you to specify commonly-used ARel queries which can be referenced To define a simple scope, we use the +scope+ method inside the class, passing the ARel query that we'd like run when this scope is called: <ruby> - class Post < ActiveRecord::Base - scope :published, where(:published => true) - end +class Post < ActiveRecord::Base + scope :published, where(:published => true) +end </ruby> Just like before, these methods are also chainable: <ruby> - class Post < ActiveRecord::Base - scope :published, where(:published => true).joins(:category) - end +class Post < ActiveRecord::Base + scope :published, where(:published => true).joins(:category) +end </ruby> Scopes are also chainable within scopes: <ruby> - class Post < ActiveRecord::Base - scope :published, where(:published => true) - scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0)) - end +class Post < ActiveRecord::Base + scope :published, where(:published => true) + scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0)) +end </ruby> To call this +published+ scope we can call it on either the class: <ruby> - Post.published => [published posts] +Post.published => [published posts] </ruby> Or on an association consisting of +Post+ objects: <ruby> - category = Category.first - category.posts.published => [published posts belonging to this category] +category = Category.first +category.posts.published => [published posts belonging to this category] </ruby> h4. Working with times |