Model:
class Contact < ActiveRecord::Base
validates :gender, :inclusion => { :in => ['male', 'female'] }
end
Migration:
class CreateContacts < ActiveRecord::Migration
def change
create_table "contacts", :force => true do |t|
t.string "gender", :limit => 6, :default => 'male'
end
end
end
RSpec test:
describe Contact do
it { should validate_inclusion_of(:gender).in(['male', 'female']) }
end
Result:
Expected Contact to be valid when gender is set to ["male", "female"]
Anybody has an idea why this spec doesn’t pass? Or can anybody reconstruct and (in)validate it? Thank you.
I misunderstood how the
.in(..)should be used. I thought I could pass an array of values, but it seems it does only accept a single value:I don’t really know what’s the difference to using
allow_valuethough:And I guess it’s always a good idea to check for some not allowed values, too:
[:no, :valid, :gender].each do |gender|
it { should_not validate_inclusion_of(:gender).in(gender) }
end