user2158382
user2158382

Reputation: 4510

Rails model validation: How to make sure a attribute is in a list

Hi I have a job model which has an string attribute called category. In the front end, I have a form with a dropbox where a user can fill out the category attribute with the selected value from the list. This is good enough front end validation for me, but now how will I do backend validation for the model?

I have dont other validations in the past for example:

validates :name, :presence => true

But is there anyway I can do something like

validates :category, :in => {"Food", "Drink", "Rental"}

Upvotes: 0

Views: 246

Answers (1)

siekfried
siekfried

Reputation: 2964

You can do it like this:

validates :category, :inclusion => { :in => %w(Food Drink Rental) }

Or shorter:

validates :category, :inclusion => %w(Food Drink Rental)

Everything is in the documentation.

Upvotes: 1

Related Questions