Skip to content

Commit

Permalink
Query guide: fix indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Dec 23, 2010
1 parent f411451 commit b4b2574
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions railties/guides/source/active_record_querying.textile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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: 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> <ruby>
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
scope :published, where(:published => true) scope :published, where(:published => true)
end end
</ruby> </ruby>


Just like before, these methods are also chainable: Just like before, these methods are also chainable:


<ruby> <ruby>
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
scope :published, where(:published => true).joins(:category) scope :published, where(:published => true).joins(:category)
end end
</ruby> </ruby>


Scopes are also chainable within scopes: Scopes are also chainable within scopes:


<ruby> <ruby>
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
scope :published, where(:published => true) scope :published, where(:published => true)
scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0)) scope :published_and_commented, published.and(self.arel_table[:comments_count].gt(0))
end end
</ruby> </ruby>


To call this +published+ scope we can call it on either the class: To call this +published+ scope we can call it on either the class:


<ruby> <ruby>
Post.published => [published posts] Post.published => [published posts]
</ruby> </ruby>


Or on an association consisting of +Post+ objects: Or on an association consisting of +Post+ objects:


<ruby> <ruby>
category = Category.first category = Category.first
category.posts.published => [published posts belonging to this category] category.posts.published => [published posts belonging to this category]
</ruby> </ruby>


h4. Working with times h4. Working with times
Expand Down

0 comments on commit b4b2574

Please sign in to comment.