aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorRyan Bigg <radarlistener@gmail.com>2011-03-22 12:16:13 +1100
committerRyan Bigg <radarlistener@gmail.com>2011-03-22 12:16:13 +1100
commitc24e5548fd372e4d0058ab417230ebc2fa3bebde (patch)
tree1dca28ad21474f02154a91388bf153d398bd76d2 /railties
parent6c309f04c9e2538cc61d6a1c753da3c6cc91b51f (diff)
downloadrails-c24e5548fd372e4d0058ab417230ebc2fa3bebde.tar.gz
rails-c24e5548fd372e4d0058ab417230ebc2fa3bebde.tar.bz2
rails-c24e5548fd372e4d0058ab417230ebc2fa3bebde.zip
Querying guide: mention that performing a where on an relation that contains an includes statement will generate a LEFT OUTER JOIN rather than an INNER JOIN or another query
Diffstat (limited to 'railties')
-rw-r--r--railties/guides/source/active_record_querying.textile14
1 files changed, 14 insertions, 0 deletions
diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile
index ed3968e226..e0fab46db9 100644
--- a/railties/guides/source/active_record_querying.textile
+++ b/railties/guides/source/active_record_querying.textile
@@ -747,6 +747,20 @@ h4. Specifying Conditions on Eager Loaded Associations
Even though Active Record lets you specify conditions on the eager loaded associations just like +joins+, the recommended way is to use "joins":#joining-tables instead.
+However if you must do this, you may use +where+ as you would normally.
+
+<ruby>
+Post.includes(:comments).where("comments.visible", true)
+</ruby>
+
+This would generate a query which contains a +LEFT OUTER JOIN+ whereas the +joins+ method would generate one using the +INNER JOIN+ function instead.
+
+<ruby>
+ SELECT "posts"."id" AS t0_r0, ... "comments"."updated_at" AS t1_r5 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE (comments.visible)
+</ruby>
+
+If in this case there were no comments for any posts, all the posts would still be loaded. By using +joins+ (an INNER JOIN), the join conditions *must* match, otherwise no records will be returned.
+
h3. Scopes
Scoping allows you to specify commonly-used ARel queries which can be referenced as method calls on the association objects or models. With these scopes, you can use every method previously covered such as +where+, +joins+ and +includes+. All scope methods will return an +ActiveRecord::Relation+ object which will allow for further methods (such as other scopes) to be called on it.