Validations on array checkbox in Model


I am using array ‘sub_id’ for multiple checkbox, i want to place validations on all array ‘sub_ids’ checbox in model just like other validations.

<% for e in @subjects do -%> <input type=“checkbox” name=“sub_ids” id=“<%= %>” value=“<%= %>”<% if @book.subjects.include? e %> checked=“checked”<%end%>> <%= %> <% end %>


class Book < ActiveRecord::Base has_and_belongs_to_many :subjects validates_presence_of:title validates_numericality_of:price validate :at_least_one_checkbox_was_ticked

protected def at_least_one_checkbox_was_ticked unless params[:sub_ids].nil? end end


I’m using only two models book and subject.

thanx in adv.

That's not how you could do it. You don't validate against params[:sub_ids], you validate against a property of the model (ie subjects.size)