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

Merge pull request #36776 from giraffate/fix_join_middle_table_alias_when_using_HABTM

Fix join middle table alias when using HABTM
This commit is contained in:
Rafael França 2019-07-26 13:57:23 -04:00 committed by GitHub
commit f825a9048f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View file

@ -62,7 +62,7 @@ module ActiveRecord::Associations::Builder # :nodoc:
def middle_reflection(join_model)
middle_name = [lhs_model.name.downcase.pluralize,
association_name].join("_").gsub("::", "_").to_sym
association_name.to_s].sort.join("_").gsub("::", "_").to_sym
middle_options = middle_options join_model
HasMany.create_reflection(lhs_model,

View file

@ -700,10 +700,17 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
assert_equal ["id"], developers(:david).projects.select(:id).first.attributes.keys
end
def test_join_middle_table_alias
assert_equal(
2,
Project.includes(:developers_projects).where.not("developers_projects.joined_on": nil).to_a.size
)
end
def test_join_table_alias
assert_equal(
3,
Developer.includes(projects: :developers).where.not("projects_developers_projects_join.joined_on": nil).to_a.size
Developer.includes(projects: :developers).where.not("developers_projects_projects_join.joined_on": nil).to_a.size
)
end
@ -716,7 +723,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
assert_equal(
3,
Developer.includes(projects: :developers).where.not("projects_developers_projects_join.joined_on": nil).group(group.join(",")).to_a.size
Developer.includes(projects: :developers).where.not("developers_projects_projects_join.joined_on": nil).group(group.join(",")).to_a.size
)
end