2010-06-10 88 views
0

我有两个班,我想建立一个多到许多assications,这里是代码:建立在Ruby中许多-to-many关联on Rails的

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 

不过,我一直得到以下错误,我会很感激,如果有人能教我什么错误:

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> 

回答

1

documentation

对于第二种方法,在两种模型中都使用has_and_belongs_to_many。这需要一个没有相应模型或主键的连接表。

您错过了此表。

+0

是的......非常感谢! – john 2010-06-10 01:28:59