提交 c30ff2b9 编写于 作者: R Ryuta Kamizono

Make association queries to preparable: Step 1

Currently association queries cannot be preparable.

```ruby
  Post.where(author_id: 1).to_a
  # => SELECT "posts".* FROM "posts" WHERE "posts"."author_id" = ?  [["author_id", 1]]

  Post.where(author: 1).to_a
  # => SELECT "posts".* FROM "posts" WHERE "posts"."author_id" = 1
```

To make association queries to preparable, it should be handled in
`create_binds_for_hash`. This change is a first step for it.
上级 7c279ad6
......@@ -77,7 +77,7 @@ def expand_from_hash(attributes)
if value.is_a?(Hash)
associated_predicate_builder(key).expand_from_hash(value)
else
expand(key, value)
build(table.arel_attribute(key), value)
end
end
end
......@@ -92,6 +92,7 @@ def create_binds_for_hash(attributes)
attrs, bvs = associated_predicate_builder(column_name).create_binds_for_hash(value)
result[column_name] = attrs
binds += bvs
next
when Relation
binds += value.bound_attributes
when Range
......@@ -113,6 +114,14 @@ 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]
......@@ -120,16 +129,6 @@ def create_binds_for_hash(attributes)
private
def expand(column, value)
# 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)
value = AssociationQueryHandler.value_for(table, column, value) if table.associated_with?(column)
build(table.arel_attribute(column), value)
end
def associated_predicate_builder(association_name)
self.class.new(table.associated_table(association_name))
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册