Topic: Trying to do a many-to-many relationship, please help with this error

AARRGHHH!!  please help again.  This time it's for sure not a typo (I think).  This is supposed to be a select menu with a many-to-many relationship.  There's two models "category.rb" and "product.rb", and both have their respective has_and_belongs_to_many.  There is a join table in the database.  I've looked at it for the last two days but I can't figure out what I'm missing.  Thanks alot this board is great, and I'm hoping at some point, maybe in the distant future it's looking, but I may be able to actually help answer questions instead of just ask them.  Let me know if I need to post anything else. 


here's the error

uninitialized constant Category
RAILS_ROOT: ./script/../config/..

Application Trace | Framework Trace | Full Trace
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in `const_missing'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:in `send'
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:in `const_missing'
#{RAILS_ROOT}/app/controllers/admin/store_controller.rb:58:in `load_data'
#{RAILS_ROOT}/app/controllers/admin/store_controller.rb:21:in `new'
-e:4:in `load'
-e:4

This error occured while loading the following files:
   admin/category.rb


and here's the store_controller
class Admin::StoreController < ApplicationController
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @product_pages, @products = paginate :products, :per_page => 10
  end

  def show
    @product = Product.find(params[:id])
    @page_title = @product.name
  end

  def new
    load_data
    @product = Product.new
  end

  def create
    @product = Product.new(params[:product])
    if @product.save
      flash[:notice] = 'Product was successfully created.'
      redirect_to :action => 'list'
    else
      load_data
      render :action => 'new'
    end
  end

  def edit
    @product = Product.find(params[:id])
  end

  def update
    @product = Product.find(params[:id])
    if @product.update_attributes(params[:product])
      flash[:notice] = 'Product was successfully updated.'
      redirect_to :action => 'show', :id => @product
    else
      render :action => 'edit'
    end
  end

  def destroy
    Product.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
 
  private
 
  def load_data
    @categories = Category.find(:all)
  end
end

Re: Trying to do a many-to-many relationship, please help with this error

Looks like it's having trouble loading the Category model. Can you post the category.rb file and the path it is located at?

Also, if you want to use a select menu for HABTM association, you should enable multiple selection. Have you done that? Post the view if you want a code sample.

Railscasts - Free Ruby on Rails Screencasts

Re: Trying to do a many-to-many relationship, please help with this error

Thanks!

Here's the category.rb

class Category < ActiveRecord::Base
  has_and_belongs_to_many :products
 
  file_column :image_url, :image_lrg
  def name
    "#{category_name}"
  end
end

the path is just "models/category.rb".  The store controller is at "controllers/admin/store_controller.rb"

here's the "view/admin/store/_form.rhtml"

<p><%= select_tag 'product[category_id][]',
    options_from_collection_for_select(@categories, :id, :name),
    {:multiple => true, :size => 5} %></p>

Thanks again.

Re: Trying to do a many-to-many relationship, please help with this error

Anyone able to see anything wrong or do I need to start looking in other files for the problem?  Thanks

Re: Trying to do a many-to-many relationship, please help with this error

I started over completely from scratch and it works now....don't ask me what is different in this one.  I don't know nor do I care.  I'm just kissing my hard drive that it works.

This is my final question and should be an easy one....I hope.  How do I display it in the Show.rhtml?  I have all the other attributes showing but I'm not sure what would call this.

<dl>
    <dt>Title</dt>
    <dd><%= @product.name %></dd>
    <dt>Description</dt>
    <dd><%= @product.description %></dd>
    <dt>Price</dt>
    <dd><%= @product.price %></dd>
    <dt>Small Image</dt>
    <dd><%= image_tag url_for_file_column(:product, :image_sm) %></dd>
    <dt>Large Image</dt>
    <dd><%= image_tag url_for_file_column(:product, :image_lg) %></dd>
    <dt>Categories</dt>
    <dd><%= WHAT GOES HERE? %></dd>
   
<%= link_to 'Edit', :action => 'edit', :id => @product %> |
<%= link_to 'Back', :action => 'list' %>

here's the _form part that got it working.
<p><label for="categories">Categories</label><br/>
<%= select_tag 'product[category_ids][]',
        options_from_collection_for_select(@categories, :id, :name),
        { :multiple => true, :size => 5} %></p>

Thanks again.