Reputation: 5392
I can't seem to create the following model:
class Rule < ActiveRecord::Base
belongs_to :product
enum comparator: [:eq, :gt, :gteq, :lt, :lteq, :not_eq, :in, :matches]
validates :key, presence: true
validates :value, presence: true
validates :comparator, inclusion: { in: [:eq, :gt, :gteq, :lt, :lteq, :not_eq, :in, :matches]}
end
Then when I go into the rails console and try to create a new Rule:
rule = Rule.new(key: "key", value: "value", comparator: :gteq)
I get the following output:
=> #<Rule:0x007ff92a4de8f8
id: nil,
key: "key",
value: "value",
comparator: 2,
default: false,
context_id: nil,
created_at: nil,
updated_at: nil>
Which looks like the object is valid. But then I run rule.valid?
and it returns false. rule.errors
returns:
#<Rule:0x007ff92a4de8f8
id: nil,
key: "key",
value: "value",
comparator: 2,
default: false,
context_id: nil,
created_at: nil,
updated_at: nil>,
@messages={:comparator=>["is not included in the list"]}>
Upvotes: 0
Views: 377
Reputation: 791
You don't need to validate your enums (I believe starting Rails 4.1+) because you will get an ArgumentError
whenever you try to assign a value not specified previously:
rule = Rule.new(key: "key", value: "value", comparator: :fff)
ArgumentError: 'fff' is not a valid comparator
Here's some discussion from the core Rails devs when they implemented the change. Hope that helps!
Upvotes: 1