# Validation on array checkbox in Model

**URL:** <https://www.ruby-forum.com/t/validation-on-array-checkbox-in-model/152792>\
**Category:** Rails\
**Created:** [November 7, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/validation-on-array-checkbox-in-model/152792 "2008-11-07T14:00:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![babar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8c91f0/32.png) [@babar](https://www.ruby-forum.com/u/babar)\
**Post date:** [November 7, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/validation-on-array-checkbox-in-model/152792/1 "2008-11-07T14:00:02Z")

</div>

Hi,

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="\<%= e.id %\>" value="\<%=  
e.id%\>"\<% if @book.subjects.include? e %\> checked=“checked”\<%end%\>\>  
\<%= e.name %\>  
\<% end %\>

MODEL

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

end

I’m using only two models book and subject.

thanx in adv.
