Establishing many-to-many associations in Ruby on Rails
I have two classes and I want to establish a many-to-many assications, here is the code:
class Category < ActiveRecord::Base
has_and_belongs_to_many :events
has_and_belongs_to_many :tips
end
class Tip < ActiveRecord::Base
has_and_belongs_to_many :categories
However, I kept getting the following errors and I would appreciate it if some one could educate me what is going wrong:
PGError: ERROR: relation "categories_tips" does not exist
: SELECT "categories".id FROM "categories" INNER JOIN "categories_tips" ON "categories".id = "categories_tips"开发者_如何转开发.category_id WHERE ("categories_tips".tip_id = NULL )
the viewer part:
4: <%= text_field :tip, :title %></label></p>
5:
6: <p><label>Categories<br/>
7: <%= select_tag('categories[]', options_for_select(Category.find(:all).collect {|c| [c.name, c.id] }, @tip.category_ids), :multiple => true ) %></label></p>
8:
9: <p><label>Location<br/>
10: <%= text_field_with_auto_complete :tip, :abstract %></label></p>
From the documentation:
For the second way, use has_and_belongs_to_many in both models. This requires a join table that has no corresponding model or primary key.
You're missing this table.
精彩评论