# HABTM check\_box + create

**URL:** <https://www.ruby-forum.com/t/habtm-check-box-create/129870>\
**Category:** Rails\
**Created:** [February 22, 2008, 7:45am UTC](https://www.ruby-forum.com/t/habtm-check-box-create/129870 "2008-02-22T07:45:21Z")\
**Posts on this page:** 2\
**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:** [February 22, 2008, 7:45am UTC](https://www.ruby-forum.com/t/habtm-check-box-create/129870/1 "2008-02-22T07:45:21Z")

</div>

Hi,

I have the following models:

Class Question  
has\_and\_belongs\_to\_many :domains  
end

Class Domain  
has\_and\_belongs\_to\_many :questions  
end

Class Category  
belongs\_to :question  
end

I have a ‘domains’ table with a list of 5 domains, and i also have a  
‘domains\_questions’ join table.

I want to be able to create a new Question and assign the Question to  
multiple Domains using a check box.

This is what i have in the questions/new.rhtml file:  
…

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

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

This is what i have in the questions\_controller.rhtml file:  
…  
def new  
@question = Question.new  
@category = Category.find(:all)  
@domain = Domain.find(:all)  
end

def create  
params[:question][:domain\_ids] ||= []  
@question = Question.new(params[:question][:category\_id]  
[:domain\_ids])  
@question.user = User.find(current\_user)  
respond\_to do |format|  
if @question.save\_with\_captcha  
flash[:notice] = ‘Question was successfully created.’  
format.html { redirect\_to question\_path(@question) }  
else  
@category = Category.find(:all)  
format.html { render :action =\> “new” }  
end  
end  
end  
…

I get this error…

NoMethodError in Questions#create  
Showing questions/new.rhtml where line #14 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.each

Extracted source (around line #14):

11:  
12:

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

  
16: \<%= check\_box\_tag “question[domain\_ids][]”, domain.id,  
@question.domains.include?(domain) %\>  
17: \<%= domain.name %\>

Does anyone know what i should be entering into the create action and  
how i should modify the view???

Many thanks,

PJ.

---

<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:** [February 22, 2008, 8:17am UTC](https://www.ruby-forum.com/t/habtm-check-box-create/129870/2 "2008-02-22T08:17:41Z")

</div>

thanks for the information i want to full structure of the project
