Skip to content

Commit

Permalink
Merge pull request #28488 from kamipo/preprocess_association_query_ha…
Browse files Browse the repository at this point in the history
…ndling

Preprocess association query handling in predicate builder
  • Loading branch information
rafaelfranca committed Mar 27, 2017
2 parents c1faca6 + e1533d2 commit 3b474da
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions activerecord/lib/active_record/relation/predicate_builder.rb
Expand Up @@ -87,14 +87,19 @@ def create_binds_for_hash(attributes)
binds = []

attributes.each do |column_name, value|
binds.concat(value.bound_attributes) if value.is_a?(Relation)
case
when value.is_a?(Hash) && !table.has_column?(column_name)
attrs, bvs = associated_predicate_builder(column_name).create_binds_for_hash(value)
result[column_name] = attrs
binds += bvs
next
when value.is_a?(Relation)
binds += value.bound_attributes
when table.associated_with?(column_name)
# 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)
result[column_name] = AssociationQueryHandler.value_for(table, column_name, value)
when value.is_a?(Range) && !table.type(column_name).respond_to?(:subtype)
first = value.begin
last = value.end
Expand All @@ -114,15 +119,6 @@ def create_binds_for_hash(attributes)
binds << build_bind_param(column_name, value)
end
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)
if table.associated_with?(column_name)
result[column_name] = AssociationQueryHandler.value_for(table, column_name, value)
end
end

[result, binds]
Expand Down Expand Up @@ -155,7 +151,6 @@ def handler_for(object)
end

def can_be_bound?(column_name, value)
return if table.associated_with?(column_name)
case value
when Array, Range
table.type(column_name).respond_to?(:subtype)
Expand Down

0 comments on commit 3b474da

Please sign in to comment.