From 3da275c4396d7fad250d2b786027ba4f14344bd4 Mon Sep 17 00:00:00 2001 From: beerlington Date: Tue, 11 Sep 2012 14:11:51 -0400 Subject: 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) --- guides/source/active_record_querying.textile | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'guides/source') 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) +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. + + +Post.where(:author => author) +Author.joins(:posts).where(:posts => {:author => author}) + + NOTE: The values cannot be symbols. For example, you cannot do +Client.where(:status => :active)+. h5(#hash-range_conditions). Range Conditions -- cgit v1.2.3