2015-11-04 49 views
0

使用Rails 4.2.1。我有一个字段是一个枚举的模型:validates_presence_of失败并返回枚举

class MyModel < ActiveRecord::Base 
    enum my_enum: { first_value: 0, second_value: 1 } 
    validates_presence_of :my_id, :username, :my_enum 
end 

在控制器中,我试图保存模型:

my_model = MyModel.find_or_initialize_by(my_id: my_id) 
my_model.update(username: username, my_enum: :first_value) 

这给了我Validation failed: My enum can't be blank

分配所要求的特性来保存,这样前对象:

my_model.username = username 
my_model.first_value! 

甚至

my_model.username = username 
my_model.my_enum = 0 
my_model.save! 

产生相同的结果。

这是否意味着validates_presence_of不能与枚举一起使用?如果确实如此,那么根本原因是什么?

回答

2

这不是一个错误。问题是my_enum列的类型是string,我试图保存一个整数。与

enum my_enum: { first_value: '0', second_value: '1' } 

更换

enum my_enum: { first_value: 0, second_value: 1 } 

固定的问题,改变了列修复它。

显然,使用字符串作为枚举是/是一个“秘密无证的功能”。有关更多详细信息,请参阅this pull request(撰写本文时尚未打开)。

+1

感谢您的信息:) –

1

我看到validates_presence_of仍然可以使用枚举。这里是我的模型

# production.rb 
class Production < ActiveRecord::Base 
    enum status: {active: 1, deactive: 0} 
    validates_presence_of :status 
end 

和我rails console

案例1测试你的情况:

2.2.0 :001 > Production.create! 
    (0.1ms) begin transaction 
    (0.1ms) rollback transaction 
ActiveRecord::RecordInvalid: Validation failed: Status can't be blank 

案例2:

2.2.0 :001 > prod = Production.find_or_initialize_by(title: "abc") 
    Production Load (0.8ms) SELECT "productions".* FROM "productions" WHERE "productions"."title" = ? LIMIT 1 [["title", "abc"]] 
=> #<Production id: nil, title: "abc", price: nil, description: nil, status: nil, created_at: nil, updated_at: nil> 
2.2.0 :002 > prod.update(price: 23, status: :active) 
    (0.3ms) begin transaction 
    SQL (0.8ms) INSERT INTO "productions" ("title", "price", "status", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["title", "abc"], ["price", 23], ["status", 1], ["created_at", "2015-11-04 05:58:43.851364"], ["updated_at", "2015-11-04 05:58:43.851364"]] 
    (0.7ms) commit transaction 
=> true 

案例3:

2.2.0 :001 > prod = Production.find_or_initialize_by(title: "def") 
    Production Load (0.6ms) SELECT "productions".* FROM "productions" WHERE "productions"."title" = ? LIMIT 1 [["title", "def"]] 
=> #<Production id: nil, title: "def", price: nil, description: nil, status: nil, created_at: nil, updated_at: nil> 
2.2.0 :002 > prod.description = "bla" 
=> "bla" 
2.2.0 :003 > prod.save! 
    (0.2ms) begin transaction 
    (0.1ms) rollback transaction 
ActiveRecord::RecordInvalid: Validation failed: Status can't be blank 

案例4:

2.2.0 :001 > prod = Production.find_or_initialize_by(title: "def") 
    Production Load (0.6ms) SELECT "productions".* FROM "productions" WHERE "productions"."title" = ? LIMIT 1 [["title", "def"]] 
=> #<Production id: nil, title: "def", price: nil, description: nil, status: nil, created_at: nil, updated_at: nil> 
2.2.0 :002 > prod.status = 0 
=> 0 
2.2.0 :003 > prod.save! 
    (0.2ms) begin transaction 
    SQL (0.8ms) INSERT INTO "productions" ("title", "status", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "def"], ["status", 0], ["created_at", "2015-11-04 06:01:16.391026"], ["updated_at", "2015-11-04 06:01:16.391026"]] 
    (0.7ms) commit transaction 
=> true 

我使用Rails 4.2.4。

+0

非常奇怪,已经案例2失败,我的验证错误。也许这是4.2.1中的一个bug已被修复? – p4sh4

+0

我也测试了这个4.2.4并取得了和你一样的结果。 – p4sh4

+0

这不是一个错误,我找到了问题,请看我的答案。谢谢您的帮助! – p4sh4