From b4b2574a12f7a3b6db97d28eda894ad872445190 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Thu, 23 Dec 2010 14:55:42 +1000 Subject: Query guide: fix indentation --- .../guides/source/active_record_querying.textile | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'railties/guides/source') 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: - class Post < ActiveRecord::Base - scope :published, where(:published => true) - end +class Post < ActiveRecord::Base + scope :published, where(:published => true) +end Just like before, these methods are also chainable: - class Post < ActiveRecord::Base - scope :published, where(:published => true).joins(:category) - end +class Post < ActiveRecord::Base + scope :published, where(:published => true).joins(:category) +end Scopes are also chainable within scopes: - 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 To call this +published+ scope we can call it on either the class: - Post.published => [published posts] +Post.published => [published posts] Or on an association consisting of +Post+ objects: - category = Category.first - category.posts.published => [published posts belonging to this category] +category = Category.first +category.posts.published => [published posts belonging to this category] h4. Working with times -- cgit v1.2.3