diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2016-08-11 22:07:36 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2016-08-11 22:18:30 +0900 |
commit | c30ff2b971eb6b0b038de20ca91ef9f3a6e460f6 (patch) | |
tree | 2eccbf6db9a155dc087800081894974bb7244852 /activerecord/lib | |
parent | 7c279ad696e1154b2f2dbef26ba0ff0a42aa105a (diff) | |
download | rails-c30ff2b971eb6b0b038de20ca91ef9f3a6e460f6.tar.gz rails-c30ff2b971eb6b0b038de20ca91ef9f3a6e460f6.tar.bz2 rails-c30ff2b971eb6b0b038de20ca91ef9f3a6e460f6.zip |
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.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 505a78cb78..692a7cbc49 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -77,7 +77,7 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 |