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

Fix has_one enum where queries

Fixes #25128
This commit is contained in:
Jon Moss 2016-05-25 20:51:37 -04:00
parent 28a7c98fe6
commit 8e2e5f9e3d
2 changed files with 20 additions and 1 deletions

View file

@ -44,7 +44,8 @@ module ActiveRecord
def associated_table(table_name)
return self if table_name == arel_table.name
association = klass._reflect_on_association(table_name)
association = klass._reflect_on_association(table_name) || klass._reflect_on_association(table_name.singularize)
if association && !association.polymorphic?
association_klass = association.klass
arel_table = association_klass.arel_table.alias(table_name)

View file

@ -659,4 +659,22 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_deprecated { firm.account(true) }
end
class SpecialBook < ActiveRecord::Base
self.table_name = 'books'
belongs_to :author, class_name: 'SpecialAuthor'
end
class SpecialAuthor < ActiveRecord::Base
self.table_name = 'authors'
has_one :book, class_name: 'SpecialBook', foreign_key: 'author_id'
end
def test_assocation_enum_works_properly
author = SpecialAuthor.create!(name: 'Test')
book = SpecialBook.create!(status: 'published')
author.book = book
refute_equal 0, SpecialAuthor.joins(:book).where(books: { status: 'published' } ).count
end
end