在这种情况下,Rails 3.1中的连接表会调用什么?

我有两个表与has_and_belongs_to_many关系: categoriesraw_categories

该表应该被称为categories_raw_categories吗?

是的,连接表以按字母顺序列出的两个要连接的表命名。 由于字母表中的categoriesraw_categories ,因此连接表称为categories_raw_categories 。 请注意,如果要进行迁移,则需要为此连接表创建单独的迁移。

有关HABTM关系及其所需的连接表的更多详细信息,请参见此处: http : //apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many

另请注意,您可以根据需要为联接表设置自定义名称。 示例(如果要调用连接表category_associations ):

 # Category model has_and_belongs_to_many :raw_categories, :join_table => 'category_associations' # RawCategory model has_and_belongs_to_many :categories, :join_table => 'category_associations' 

您也可以使用has_many :though显式地使连接表成为第一类模型has_many :though在要连接的模型上。 按照上面的示例,您可以将CategoryAssociation设为实际模型,并将其连接到其他两个,如下所示:

 # CateogoryAssociation model belongs_to :category belongs_to :raw_category # Category model has_many :category_associations has_many :raw_categories, :through => :category_associations # RawCategory model has_many :category_associations has_many :categories, :through => :category_associations