diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-08-12 03:37:43 +0900 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2017-08-11 14:37:43 -0400 |
commit | 8f5e8886179f7cbc162f546a7d73f55d9296e06c (patch) | |
tree | a10994a1230965757bb98c0ecd5d8b3723d2dd68 /activerecord/lib/active_record | |
parent | 67f1b51f8518577b1bb044a3f5da6279975f0461 (diff) | |
download | rails-8f5e8886179f7cbc162f546a7d73f55d9296e06c.tar.gz rails-8f5e8886179f7cbc162f546a7d73f55d9296e06c.tar.bz2 rails-8f5e8886179f7cbc162f546a7d73f55d9296e06c.zip |
Specify `table.name` only when `scope.table` and `table` are different (#29058)
Fixes #29045.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/association_scope.rb | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 9b0b50977d..3d79e540b8 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -68,11 +68,11 @@ module ActiveRecord foreign_key = join_keys.foreign_key value = transform_value(owner[foreign_key]) - scope = scope.where(table.name => { key => value }) + scope = apply_scope(scope, table, key, value) if reflection.type polymorphic_type = transform_value(owner.class.base_class.name) - scope = scope.where(table.name => { reflection.type => polymorphic_type }) + scope = apply_scope(scope, table, reflection.type, polymorphic_type) end scope @@ -91,10 +91,10 @@ module ActiveRecord if reflection.type value = transform_value(next_reflection.klass.base_class.name) - scope = scope.where(table.name => { reflection.type => value }) + scope = apply_scope(scope, table, reflection.type, value) end - scope = scope.joins(join(foreign_table, constraint)) + scope.joins!(join(foreign_table, constraint)) end class ReflectionProxy < SimpleDelegator # :nodoc: @@ -165,6 +165,14 @@ module ActiveRecord scope end + def apply_scope(scope, table, key, value) + if scope.table == table + scope.where!(key => value) + else + scope.where!(table.name => { key => value }) + end + end + def eval_scope(reflection, table, scope, owner) reflection.build_scope(table).instance_exec(owner, &scope) end |