# HABTM join table issues

**URL:** <https://www.ruby-forum.com/t/habtm-join-table-issues/225946>\
**Category:** Rails\
**Created:** [April 24, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/habtm-join-table-issues/225946 "2007-04-24T23:12:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rajo](https://www.ruby-forum.com/u/rajo)\
**Post date:** [April 24, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/habtm-join-table-issues/225946/1 "2007-04-24T23:12:21Z")

</div>

Hello, I am currently working on my first rails app which is a product  
catalog. I am trying to show products by their associated categories  
and sections. I am able to save the these categories and section ids to  
the product but not exactly vice versa, so I can have users view a  
category/section page that shows all of the corresponding products.

I am trying to use a join table currently with a HABTM :products  
association for my section model and a belongs\_to :section association  
for the product model and the join table is named “products\_sections”  
and consists of a “product\_id” column and a “section\_id” column.

In my products controller I can currently append the changed section to  
the joined table but it wont delete the previous record, leaving me  
with a product that is in two sections in the join table. My update  
action looks like the following…

def update  
@product = Product.find(params[:id])  
old\_section = @product.section\_id  
@products = Product.find(:all)  
if @section = Section.find\_by\_id(params[:section\_id])  
@product.section = @section  
unless @section.products.include?(@product)  
@section.products \<\< @product  
@section.products.delete(old\_section)  
end  
end  
respond\_to do |format|  
if @product.update\_attributes(params[:product])  
flash[:notice] = ‘Product was successfully updated.’  
format.html { redirect\_to :action =\> ‘show’, :controller =\>  
‘products’, :id =\> @product }  
format.xml { head :ok }  
else  
@product = Product.find(params[:id])  
@sections = Section.find(:all)  
format.html { render :action =\> “edit” }  
format.xml { render :xml =\> @product.errors.to\_xml }  
end  
end  
end

here’s my section model

class Section \< ActiveRecord::Base  
set\_table\_name “sections”  
set\_primary\_key “id”

validates\_presence\_of :name  
validates\_uniqueness\_of :name  
validates\_length\_of :name, :minimum =\> 2

validates\_uniqueness\_of :permalink

has\_and\_belongs\_to\_many :products, :class\_name =\> “Product”,  
:foreign\_key =\> “section\_id”, :join\_table =\> “products\_sections”,  
:association\_foreign\_key =\> “product\_id”

def before\_create  
@attributes[‘permalink’] =  
name.downcase.gsub(/(\s&\s)|(\s&amp;\s)/, ’ and  
‘).gsub(/[’]|(\s&#39;\s)/, ‘’).gsub(/\W/, ’ ').gsub(/\ +/,  
‘-’).gsub(/(_)$/, ‘’).gsub(/^(_)/, ‘’)  
end

def before\_update  
@attributes[‘permalink’] =  
name.downcase.gsub(/(\s&\s)|(\s&amp;\s)/, ’ and  
‘).gsub(/[’]|(\s&#39;\s)/, ‘’).gsub(/\W/, ’ ').gsub(/\ +/,  
‘-’).gsub(/(_)$/, ‘’).gsub(/^(_)/, ‘’)  
end

def to\_param  
permalink  
end  
end

and here’s my product model

class Product \< ActiveRecord::Base  
set\_table\_name “products”  
set\_primary\_key “id”  
acts\_as\_textiled :description

image\_column :photo, :versions =\> { :thumb =\> “c100x100” }  
image\_column :photo\_two, :versions =\> { :thumb =\> “c100x100” }

validates\_presence\_of :name  
validates\_uniqueness\_of :name  
validates\_length\_of :name, :minimum =\> 4

validates\_uniqueness\_of :permalink

validates\_inclusion\_of :gender, :in =\> [‘Male’, ‘Female’, ‘Unisex’],  
:message =\> “please pick a gender”

validates\_presence\_of :section\_id

belongs\_to :section, :class\_name =\> “Section”, :foreign\_key =\>  
“section\_id”

def before\_create  
@attributes[‘permalink’] =  
name.downcase.gsub(/(\s&\s)|(\s&amp;\s)/, ’ and  
‘).gsub(/[’]|(\s&#39;\s)/, ‘’).gsub(/\W/, ’ ').gsub(/\ +/,  
‘-’).gsub(/(_)$/, ‘’).gsub(/^(_)/, ‘’)  
end

def to\_param  
“#{id}-#{permalink}”  
end  
end

and here is my update form

  

```
<p><label for="product_name">Name</label><br />
<%= text_field 'product', 'name' %></p>

<br />

<p><label for="product_description">Description</label><br />
<%= textile_editor 'product', 'description', :style => "width:

```

390px; height: 200px" -%\>

```
<div class="form_photo">
    <p><label for="product_photo">Photo One</label><br />
    <% if @product.photo %><%= image_tag @product.photo.thumb.url

```

%\>\<% end %\>  
  
  
\<%= upload\_column\_field(‘product’, ‘photo’) %\>

  

```
<div class="form_photo">
    <p><label for="product_photo">Photo Two</label><br />
    <% if @product.photo_two %><%= image_tag

```

@product.photo\_two.thumb.url %\>\<% end %\>  
  
  
\<%= upload\_column\_field(‘product’, ‘photo\_two’) %\>

  

```
<p><label for="product_section_id">Section</label><br/>
<%= select_tag('section_id', options_for_select([['Choose One', '']]

```

- @sections.collect {|section| [section.name, section.id]},  
@product.section\_id)) %\>

\<%= textile\_editor\_initialize -%\>

I’m sure there is something off with my function/syntax here but maybe  
the HABTM association model is not the best way to go about this? Any  
help would be great, I’m using rails 1.2.3 and mongrel. Any help would  
be greatly appreciated.
