ruby-on-rails – 在Rails 3.1中,这种情况下连接表会调用什么?
发布时间:2020-12-17 02:30:36 所属栏目:百科 来源:网络整理
导读:我有两个表与has_and_belongs_to_many关系:categories和raw_categories 该表应该被称为categories_raw_categories吗? 解决方法 是的,连接表以按字母顺序列出的两个要连接的表命名.由于字母表中的类别比raw_categories更高,因此连接表称为categories_raw_ca
我有两个表与has_and_belongs_to_many关系:categories和raw_categories
该表应该被称为categories_raw_categories吗? 解决方法
是的,连接表以按字母顺序列出的两个要连接的表命名.由于字母表中的类别比raw_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显式地使连接表成为第一类模型:尽管在要连接的模型上.按照上面的示例,您可以将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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |