# Validation of models

**URL:** <https://www.ruby-forum.com/t/validation-of-models/140063>\
**Category:** Rails\
**Created:** [June 10, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/validation-of-models/140063 "2008-06-10T14:59:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweetpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sweetpete](https://www.ruby-forum.com/u/sweetpete)\
**Post date:** [June 10, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/validation-of-models/140063/1 "2008-06-10T14:59:28Z")

</div>

I have 2 models;

1. Question : has\_and\_belongs\_to\_many :domains

2. Domain : has\_and\_belongs\_to\_many :questions

When a user submits a new question, the domains appear as a list of  
check boxes to select, here is the new question form:

# New Question
\<%= error\_messages\_for :question, :header\_message =\> "Please try again!", :message =\> "Amend the following errors:" %\> \<% form\_for(:question, :url =\> questions\_path) do |f| %\>

Domain:  
\<% @domain.each do |domain| %\>

\<%= check\_box\_tag "question[domain\_ids][]", domain.id, @question.domains.include?(domain) %\> \<%= h(domain.name) %\> \<%= image\_tag("#{domain.imagename}")%\>
\<% end %\>

Question:  
(Keep it short and simple!)  
\<%= f.text\_field :question, :size =\> '60' %\>

\<%= submit\_tag "Submit" %\>

\<% end %\>

My question is how to ensure that a user selects a domain in order for  
the question to be successfully submitted.

In the question model i have tried:

validates\_presence\_of :domain

But that does not quite work. If a domain is not selected the question  
will not be created, however if the user does select a domain, i still  
get a validation error “Domain can’t be blank”!

Any help appreciated,

PJ.
