Model validation and Aray

How to validate a aray in model?

In form I have a multiple select option, that returns a array

(in model I convert array to string and save it to database)

array can only have values ['A','B','C','D','E','']

So how can I valdate that some value is not a X or Y ?

The model code is:

   attr_accessor :categories

   def categories      if self.category        @categories = self.category.split     else       @categories = ['']     end    end

   def categories=(xxx)      @categories = xxx      return if xxx.blank?      self.category = xxx.join(" ")    end

Use a custom validator method that looks at the elements. See:

  http://guides.rubyonrails.org/active_record_validations_callbacks.html#custom-validators

and use an array of bad elements with a method like so:

  badElts = [:put, :bad, :element, :values, :here]

  def validate record     record.array_of_stuff.each do |elt|       record.errors[:name] << "'#{elt}' is forbidden" if badElts.include? elt     end   end

-Dave