# HABTM & CheckBoxes

**URL:** <https://www.ruby-forum.com/t/habtm-checkboxes/150775>\
**Category:** Rails\
**Created:** [October 13, 2008, 7:20am UTC](https://www.ruby-forum.com/t/habtm-checkboxes/150775 "2008-10-13T07:20:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Davo](https://www.ruby-forum.com/u/Davo)\
**Post date:** [October 13, 2008, 7:20am UTC](https://www.ruby-forum.com/t/habtm-checkboxes/150775/1 "2008-10-13T07:20:05Z")

</div>

I’m trying to achieve something that I assume is fairly common?

I have a Category table and a Brand Table.  
When I create a new Category I want a list of checkboxes to appear,  
and when checked they update the category\_brand table.

Here are the models.  
class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :brands  
end  
class Brand \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :categories  
end

Here is the view to add a new category.

# Add a new Category
\<% form\_for(@category) do |f| %\> \<%= f.error\_messages %\> 

\<%= f.label :name %\>  
 \<%= f.text\_field :name %\>

### Add Brands required for this new category

 \<% for b in @brands do -%\> - \<%= check\_box\_tag "categories[brand\_ids][]", "#{b.id}" -%\> \<%= "#{b.name}" -%\> 
 \<% end -%\> 

\<%= f.submit "Create" %\>

\<% end %\>

&nbsp;

\<%= link\_to 'Back', categories\_path %\>

I have read all the documentation & tutorials I can find on this and  
it is not clear to me if anything has to be done in the controller?  
Some examples imply it happens because of the association and other  
example have code such as:  
@category.brands = brand.find(@params[:brand\_ids]) if  
@params[:brand\_ids]  
Which produces an error for me…  
You have a nil object when you didn’t expect it!

TIA - Dave P.

---

<div class="post-metadata">

**Author:** ![Davo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Davo](https://www.ruby-forum.com/u/Davo)\
**Post date:** [October 13, 2008, 7:27am UTC](https://www.ruby-forum.com/t/habtm-checkboxes/150775/2 "2008-10-13T07:27:45Z")

</div>

Davo wrote:

> @category.brands = brand.find(@params[:brand\_ids]) if @params[:categories][:brand\_ids]

check this

---

<div class="post-metadata">

**Author:** ![Davo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Davo](https://www.ruby-forum.com/u/Davo)\
**Post date:** [October 13, 2008, 7:28am UTC](https://www.ruby-forum.com/t/habtm-checkboxes/150775/3 "2008-10-13T07:28:11Z")

</div>

Pokkai D. wrote:

> Davo wrote:
> 
> > @category.brands = brand.find(@params[:categories][:brand\_ids]) if @params[:categories][:brand\_ids]
> 
> check this

---

<div class="post-metadata">

**Author:** ![Davo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Davo](https://www.ruby-forum.com/u/Davo)\
**Post date:** [October 13, 2008, 7:52am UTC](https://www.ruby-forum.com/t/habtm-checkboxes/150775/4 "2008-10-13T07:52:17Z")

</div>

All working, no code required in controller  
( other than code mentioned in RailsCasts HABTM  
[#17 HABTM Checkboxes - RailsCasts](http://railscasts.com/episodes/17-habtm-checkboxes) )

I had the naming wrong for the brands\_categories table !

cheers, Dave

On Oct 13, 1:28 pm, Pokkai D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
