Primary key is not allowed in a has_and_belongs_to_many join table (arts_pages).
由于项目需要使用了has_and_belongs_to_many,在执行代码:
n = Art.find 22
n.pages << Page.new
时出现下面错误:
Primary key is not allowed in a has_and_belongs_to_many join table (arts_pages)
原来错误的原因是在我创建的中间表:arts_pages中多了一个id字段
修改办法:
create_table :arts_pages , :id => false do |t|
本文介绍了一种常见的Rails应用错误——在使用has_and_belongs_to_many关联时,中间表不应该包含主键id。通过一个具体示例说明了如何定位此问题并提供了解决方案。
2810

被折叠的 条评论
为什么被折叠?



