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\&\;\s)/, '' and '').gsub(/['']|(\s\'\;\s)/, '''').gsub(/\W/, '' '').gsub(/\ +/, ''-'').gsub(/(_)$/, '''').gsub(/^(_)/, '''') end def before_update @attributes[''permalink''] name.downcase.gsub(/(\s\&\s)|(\s\&\;\s)/, '' and '').gsub(/['']|(\s\'\;\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\&\;\s)/, '' and '').gsub(/['']|(\s\'\;\s)/, '''').gsub(/\W/, '' '').gsub(/\ +/, ''-'').gsub(/(_)$/, '''').gsub(/^(_)/, '''') end def to_param "#{id}-#{permalink}" end end and here is my update form <!--[form:product]--> <br /> <div id="form_left"> <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" -%></p> <div class="form_photo"> <p><label for="product_photo">Photo One</label><br /> <% if @product.photo %><%= image_tag @product.photo.thumb.url %><% end %> <br /> <%= upload_column_field(''product'', ''photo'') %></p> </div> <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 %> <br /> <%= upload_column_field(''product'', ''photo_two'') %></p> </div> </div> <div id="form_right"> <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)) %></p> <p><label for="product_gender">Gender</label><br /> <%= select ''product'', ''gender'', select_genders %></p> <% if @product.permalink %> <p><label for="product_permalink">Permalink</label><br /> <%= text_field ''product'', ''permalink'', :style => "width: 120px" %> </p> <% end %> </div> <%= textile_editor_initialize -%> <!--[eoform:product]--> 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---