HABTM join table issues

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)) %>

    Gender
    <%= select 'product', 'gender', select_genders %>

    <% if @product.permalink %>

    Permalink
    <%= text_field 'product', 'permalink', :style => "width: 120px" %>

    <% end %>
<%= 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs