rails validate in model that value is inside array

arrays, model, ruby-on-rails, text, validation

Solution

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 }

Problem

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

Original source