aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-09-12 14:04:11 -0700
committerJon Leighton <j@jonathanleighton.com>2012-09-12 14:04:11 -0700
commitb5aed34c442550c4caad1fdca12c921d5ab9840f (patch)
treee19d1e7147070f25fcd18754211913552f99dde1 /activerecord/lib/active_record/relation/query_methods.rb
parent0f228b420e26584e26702935135a4b099df85696 (diff)
parent3da275c4396d7fad250d2b786027ba4f14344bd4 (diff)
downloadrails-b5aed34c442550c4caad1fdca12c921d5ab9840f.tar.gz
rails-b5aed34c442550c4caad1fdca12c921d5ab9840f.tar.bz2
rails-b5aed34c442550c4caad1fdca12c921d5ab9840f.zip
Merge pull request #7273 from beerlington/foreign_key_model_queries
Convert model name to foreign key in queries
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index f6bacf4822..00cdaf686f 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -340,6 +340,24 @@ module ActiveRecord
# User.where({ created_at: (Time.now.midnight - 1.day)..Time.now.midnight })
# # SELECT * FROM users WHERE (created_at BETWEEN '2012-06-09 07:00:00.000000' AND '2012-06-10 07:00:00.000000')
#
+ # 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.
+ #
+ # author = Author.find(1)
+ #
+ # # The following queries will be equivalent:
+ # Post.where(:author => author)
+ # Post.where(:author_id => author)
+ #
+ # This also works with polymorphic belongs_to relationships:
+ #
+ # treasure = Treasure.create(:name => 'gold coins')
+ # treasure.price_estimates << PriceEstimate.create(:price => 125)
+ #
+ # # The following queries will be equivalent:
+ # PriceEstimate.where(:estimate_of => treasure)
+ # PriceEstimate.where(:estimate_of_type => 'Treasure', :estimate_of_id => treasure)
+ #
# === Joins
#
# If the relation is the result of a join, you may create a condition which uses any of the