aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorbeerlington <pete@lette.us>2012-09-11 14:11:51 -0400
committerbeerlington <pete@lette.us>2012-09-11 14:11:51 -0400
commit3da275c4396d7fad250d2b786027ba4f14344bd4 (patch)
treea0367f680705af28ea053e890f82b359fa511a11 /guides
parentf2a44ade14f995d0574bedf79c8d1bee520f0306 (diff)
downloadrails-3da275c4396d7fad250d2b786027ba4f14344bd4.tar.gz
rails-3da275c4396d7fad250d2b786027ba4f14344bd4.tar.bz2
rails-3da275c4396d7fad250d2b786027ba4f14344bd4.zip
Accept belongs_to assoc. keys in ActiveRecord queries
Allows you to specify the model association key in a belongs_to relationship instead of the foreign key. The following queries are now equivalent: Post.where(:author_id => Author.first) Post.where(:author => Author.first) PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure) PriceEstimate.where(:estimate_of => treasure)
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_record_querying.textile7
1 files changed, 7 insertions, 0 deletions
diff --git a/guides/source/active_record_querying.textile b/guides/source/active_record_querying.textile
index 96ae5b2972..e21b86721f 100644
--- a/guides/source/active_record_querying.textile
+++ b/guides/source/active_record_querying.textile
@@ -465,6 +465,13 @@ The field name can also be a string:
Client.where('locked' => true)
</ruby>
+In the case of a belongs_to relationship, an association key can be used to specify the model if an ActiveRecord object is used as the value. This method works with polymorphic relationships as well.
+
+<ruby>
+Post.where(:author => author)
+Author.joins(:posts).where(:posts => {:author => author})
+</ruby>
+
NOTE: The values cannot be symbols. For example, you cannot do +Client.where(:status => :active)+.
h5(#hash-range_conditions). Range Conditions