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) --- .../active_record/relation/predicate_builder.rb | 52 ++++++++++++++++++++-- .../lib/active_record/relation/query_methods.rb | 18 ++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index cb8f903474..593de71fb3 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -1,12 +1,25 @@ module ActiveRecord class PredicateBuilder # :nodoc: def self.build_from_hash(engine, attributes, default_table) - attributes.map do |column, value| + queries = [] + + attributes.each do |column, value| table = default_table if value.is_a?(Hash) table = Arel::Table.new(column, engine) - value.map { |k,v| build(table[k.to_sym], v) } + + value.each do |k,v| + if rk = find_reflection_key(column, v, v) + if rk[:foreign_type] + queries << build(table[rk[:foreign_type]], v.class.base_class) + end + + k = rk[:foreign_key] + end + + queries << build(table[k.to_sym], v) + end else column = column.to_s @@ -15,9 +28,19 @@ module ActiveRecord table = Arel::Table.new(table_name, engine) end - build(table[column.to_sym], value) + if rk = find_reflection_key(column, engine, value) + if rk[:foreign_type] + queries << build(table[rk[:foreign_type]], value.class.base_class) + end + + column = rk[:foreign_key] + end + + queries << build(table[column.to_sym], value) end - end.flatten + end + + queries end def self.references(attributes) @@ -31,6 +54,27 @@ module ActiveRecord end.compact end + # Find the foreign key when using queries such as: + # Post.where(:author => author) + # + # For polymorphic relationships, find the foreign key and type: + # PriceEstimate.where(:estimate_of => treasure) + def self.find_reflection_key(parent_column, model, value) + # value must be an ActiveRecord object + return nil unless value.class < Model::Tag + + if reflection = model.reflections[parent_column.to_sym] + if reflection.options[:polymorphic] + { + :foreign_key => reflection.foreign_key, + :foreign_type => reflection.foreign_type + } + else + { :foreign_key => reflection.foreign_key } + end + end + end + private def self.build(attribute, value) case value 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 -- cgit v1.2.3