HABTM with multiple select list

Inessa Weiner
2006-06-29T19:54:22Z

Hello,

I am very new to ruby and need to create my first HABTM with multiple
select list.

 I am having problems saving data in the middle(linking)
table.

 1. titles can have multiple categories

 2. categories can have multiple titles

Categories, titles and categories_titles tables are created.

 ----- title_controller.rb--

  def new

    @title = Title.new

    @categories = Category.find(:all, :order
=&gt;"name")

    @category_ids = []

    @title.categories.each { |x| @category_ids &lt;&lt;
x.id }

  end

  def create

    @title = Title.new(params[:title])

    @title.categories &lt;&lt;
Category.find(@params[:category_ids])

    if @title.save

      flash[:notice] = ''Title was successfully
created.''

      redirect_to :action =&gt; ''list''

    else

      render :action =&gt; ''new''

    end

  end

  ----title.rb---

  class Title &lt; ActiveRecord::Base

    has_and_belongs_to_many :categories

end

  -------------category.rb--

  class Category &lt; ActiveRecord::Base

  validates_presence_of :name

  has_and_belongs_to_many :titles

end

---

----view

Category Mappings:

&lt;% options = options_from_collection_for_select (@categories,
''name'',''id'', @category_ids) %&gt;

&lt;%= select_tag(''categories_ids[]'', options,
{:multiple=&gt;true,
:size=&gt;7}) %&gt;

----

I am getting error:

Couldn''t find Category without an ID

Please help.

Thanks.

Inessa.


_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org