diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-07-27 23:23:05 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-27 23:23:05 -0300 |
commit | 70ec7feaecab2d3f4dcb84ef2373a80d427aa336 (patch) | |
tree | 1f589c86d0706cb37d95a2d56a51758cf2244266 /activerecord | |
parent | ea1dcbbc9965f82361f45d5b098207de738d7bb9 (diff) | |
parent | b7d229d1fabbd271fd37e9f4eac316a44d690842 (diff) | |
download | rails-70ec7feaecab2d3f4dcb84ef2373a80d427aa336.tar.gz rails-70ec7feaecab2d3f4dcb84ef2373a80d427aa336.tar.bz2 rails-70ec7feaecab2d3f4dcb84ef2373a80d427aa336.zip |
Merge pull request #25767 from kamipo/association_name_is_the_same_as_join_table_name
Correctly return `associated_table` when `associated_with?` is true
Diffstat (limited to 'activerecord')
4 files changed, 20 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index a1326aa359..5fe0d8b5e4 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -42,11 +42,11 @@ module ActiveRecord end def associated_table(table_name) - return self if table_name == arel_table.name - association = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.singularize) - if association && !association.polymorphic? + if !association && table_name == arel_table.name + return self + elsif association && !association.polymorphic? association_klass = association.klass arel_table = association_klass.arel_table.alias(table_name) else diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index e27f16b6e4..a959f3c06a 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -19,6 +19,7 @@ require 'models/professor' require 'models/treasure' require 'models/price_estimate' require 'models/club' +require 'models/user' require 'models/member' require 'models/membership' require 'models/sponsor' @@ -995,4 +996,9 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase Project.first.developers_required_by_default.create!(name: "Sean", salary: 50000) end end + + def test_association_name_is_the_same_as_join_table_name + user = User.create! + assert_nothing_raised { user.jobs_pool.clear } + end end diff --git a/activerecord/test/models/user.rb b/activerecord/test/models/user.rb index f5dc93e994..97107a35a0 100644 --- a/activerecord/test/models/user.rb +++ b/activerecord/test/models/user.rb @@ -1,6 +1,12 @@ +require 'models/job' + class User < ActiveRecord::Base has_secure_token has_secure_token :auth_token + + has_and_belongs_to_many :jobs_pool, + class_name: Job, + join_table: 'jobs_pool' end class UserWithNotification < User diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 2f2993ce18..a5155e9cdb 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -390,6 +390,11 @@ ActiveRecord::Schema.define do t.integer :ideal_reference_id end + create_table :jobs_pool, force: true, id: false do |t| + t.references :job, null: false, index: true + t.references :user, null: false, index: true + end + create_table :keyboards, force: true, id: false do |t| t.primary_key :key_number t.string :name |