1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #25767 from kamipo/association_name_is_the_same_as_join_table_name

Correctly return `associated_table` when `associated_with?` is true
This commit is contained in:
Rafael França 2016-07-27 23:23:05 -03:00 committed by GitHub
commit 70ec7feaec
4 changed files with 20 additions and 3 deletions

View file

@ -42,11 +42,11 @@ module ActiveRecord
end end
def associated_table(table_name) 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) 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 association_klass = association.klass
arel_table = association_klass.arel_table.alias(table_name) arel_table = association_klass.arel_table.alias(table_name)
else else

View file

@ -19,6 +19,7 @@ require 'models/professor'
require 'models/treasure' require 'models/treasure'
require 'models/price_estimate' require 'models/price_estimate'
require 'models/club' require 'models/club'
require 'models/user'
require 'models/member' require 'models/member'
require 'models/membership' require 'models/membership'
require 'models/sponsor' require 'models/sponsor'
@ -995,4 +996,9 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
Project.first.developers_required_by_default.create!(name: "Sean", salary: 50000) Project.first.developers_required_by_default.create!(name: "Sean", salary: 50000)
end end
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 end

View file

@ -1,6 +1,12 @@
require 'models/job'
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_secure_token has_secure_token
has_secure_token :auth_token has_secure_token :auth_token
has_and_belongs_to_many :jobs_pool,
class_name: Job,
join_table: 'jobs_pool'
end end
class UserWithNotification < User class UserWithNotification < User

View file

@ -390,6 +390,11 @@ ActiveRecord::Schema.define do
t.integer :ideal_reference_id t.integer :ideal_reference_id
end 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| create_table :keyboards, force: true, id: false do |t|
t.primary_key :key_number t.primary_key :key_number
t.string :name t.string :name