rails validate in model that value is inside array

I have a form where i pass a field named :type and i want to check if it's value is inside an array of allowed types so that no one is allowed to post not-allowed types.

the array looks like

@allowed_types = [
   'type1',
   'type2',
   'type3',
   'type4',
   'type5',
   'type6',
   'type7',
   etc...
]

i have tried using validates_exclusion_of or validates_inclusion_of but it doesn't seem to work

Answers


first, change the attribute from type to something else, type is a reserved attrubute name use for Single Table Inheritance and such.

class Thing < ActiveRecord::Base
   validates :mytype, :inclusion=> { :in => @allowed_types }

Need Your Help

SQL Storing Age Range

sql sql-server tsql optimization database-design

Im building an friendship site and I want to store restrictions for when a profile can be viewed.

Error and not receiving email from contact form

php html ajax

i am trying to create a contact form that just send the form to my email, i am having a couple of problems with it, when the user submits the form it responds with an error but also the message say...