aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRobin Dupret <robin.dupret@gmail.com>2013-12-30 16:56:13 +0100
committerRobin Dupret <robin.dupret@gmail.com>2014-01-01 17:57:34 +0100
commit7aebcb67b040bca353cd2e97a468c8ffe4594665 (patch)
tree0f1a7aa25f08d91083686ada12d3771c0d7c8df2 /activerecord/test
parent97ddfb48dbab8d9410e473f3f6e7e2d189b97b4f (diff)
downloadrails-7aebcb67b040bca353cd2e97a468c8ffe4594665.tar.gz
rails-7aebcb67b040bca353cd2e97a468c8ffe4594665.tar.bz2
rails-7aebcb67b040bca353cd2e97a468c8ffe4594665.zip
Fix the enums writer methods
Previously, the writer methods would simply check whether the passed argument was the symbol representing the integer value of an enum field. Therefore, it was not possible to specify the numeric value itself but the dynamically defined scopes generate where clauses relying on this kind of values so a chained call to a method like `find_or_initialize_by` would trigger an `ArgumentError`. Reference #13530
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/enum_test.rb11
-rw-r--r--activerecord/test/schema/schema.rb4
2 files changed, 14 insertions, 1 deletions
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index 017edcb194..47e3dfc3ad 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -16,7 +16,7 @@ class EnumTest < ActiveRecord::TestCase
assert @book.unread?
end
- test "query state with symbol" do
+ test "query state with strings" do
assert_equal "proposed", @book.status
assert_equal "unread", @book.read_status
end
@@ -78,4 +78,13 @@ class EnumTest < ActiveRecord::TestCase
assert_equal 1, Book::STATUS["written"]
assert_equal 2, Book::STATUS[:published]
end
+
+ test "first_or_initialize with enums' scopes" do
+ class Issue < ActiveRecord::Base
+ enum status: [:open, :closed]
+ end
+
+ assert Issue.open.empty?
+ assert Issue.open.first_or_initialize
+ end
end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index ac546fc296..9f504801af 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -327,6 +327,10 @@ ActiveRecord::Schema.define do
t.string :color
end
+ create_table :issues, force: true do |t|
+ t.integer :status
+ end
+
create_table :items, force: true do |t|
t.column :name, :string
end