aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRich <jychen7@users.noreply.github.com>2018-08-25 19:58:48 +0800
committerRich <jychen7@users.noreply.github.com>2018-08-25 19:58:48 +0800
commiteed8b23318c03feb403498fcdea40edeee0532dc (patch)
tree4a5eff433b05f2bc2b5d9ef94e9f32761279c97f
parenta0b57bbb21ce81071220bd8c6cfd8cdda342c6c6 (diff)
downloadrails-eed8b23318c03feb403498fcdea40edeee0532dc.tar.gz
rails-eed8b23318c03feb403498fcdea40edeee0532dc.tar.bz2
rails-eed8b23318c03feb403498fcdea40edeee0532dc.zip
Add test case to test enum in has_many
There is test in has_one to test enum, but there is no for has_many. [Rich Chen]
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 5e6bea17ea..47b24a0b46 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -12,6 +12,7 @@ require "models/category"
require "models/image"
require "models/post"
require "models/author"
+require "models/book"
require "models/essay"
require "models/comment"
require "models/person"
@@ -377,6 +378,27 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal invoice.id, line_item.invoice_id
end
+ class SpecialAuthor < ActiveRecord::Base
+ self.table_name = "authors"
+ has_many :books, class_name: "SpecialBook", foreign_key: :author_id
+ end
+
+ class SpecialBook < ActiveRecord::Base
+ self.table_name = "books"
+
+ belongs_to :author
+ enum read_status: { unread: 0, reading: 2, read: 3, forgotten: nil }
+ end
+
+ def test_association_enum_works_properly
+ author = SpecialAuthor.create!(name: "Test")
+ book = SpecialBook.create!(read_status: "reading")
+ author.books << book
+
+ assert_equal "reading", book.read_status
+ assert_not_equal 0, SpecialAuthor.joins(:books).where(books: { read_status: "reading" }).count
+ end
+
# When creating objects on the association, we must not do it within a scope (even though it
# would be convenient), because this would cause that scope to be applied to any callbacks etc.
def test_build_and_create_should_not_happen_within_scope