diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-09-12 14:04:11 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-09-12 14:04:11 -0700 |
commit | b5aed34c442550c4caad1fdca12c921d5ab9840f (patch) | |
tree | e19d1e7147070f25fcd18754211913552f99dde1 /activerecord/lib/active_record/relation/predicate_builder.rb | |
parent | 0f228b420e26584e26702935135a4b099df85696 (diff) | |
parent | 3da275c4396d7fad250d2b786027ba4f14344bd4 (diff) | |
download | rails-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/predicate_builder.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 52 |
1 files changed, 48 insertions, 4 deletions
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 |