diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2016-05-26 20:11:13 -0400 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2016-05-26 20:11:13 -0400 |
commit | 0a571e4ad416a72a5827d4bcca5e4f6e8920ec93 (patch) | |
tree | 159081937248358fd3c2313e6672bf78afc59b28 /activerecord | |
parent | 8b64b7f99dcb6ec30fad883e1e92587074e43812 (diff) | |
parent | 8e2e5f9e3d1f434e265dc104ea9b00ff75702fc3 (diff) | |
download | rails-0a571e4ad416a72a5827d4bcca5e4f6e8920ec93.tar.gz rails-0a571e4ad416a72a5827d4bcca5e4f6e8920ec93.tar.bz2 rails-0a571e4ad416a72a5827d4bcca5e4f6e8920ec93.zip |
Merge pull request #25146 from maclover7/fix-25128
Fix `has_one` `enum` `where` queries
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/table_metadata.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_one_associations_test.rb | 18 |
2 files changed, 20 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/table_metadata.rb b/activerecord/lib/active_record/table_metadata.rb index 0faad48ce3..a1326aa359 100644 --- a/activerecord/lib/active_record/table_metadata.rb +++ b/activerecord/lib/active_record/table_metadata.rb @@ -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) diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index c9d9e29f09..1574f373c2 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -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 |