【发布时间】:2012-11-30 17:59:12
【问题描述】:
我正在使用 Dr.Nic 的轨道复合主键 (http://compositekeys.rubyforge.org/)
在示例中,他有 has_many 和 belongs_to 关系,但没有 has_and_belongs_to_many
我的关联从书籍到流派很好(书籍具有标题和作者的复合初级键),但流派到书籍尝试查询连接表中不存在的 book_id 列,并引发错误。
class Book < ActiveRecord::Base
self.primary_keys = :title, :author
has_and_belongs_to_many :genres, foreign_key: [:title, :author]
end
class Genre < ActiveRecord::Base
has_and_belongs_to_many :books, foreign_key: [:title, :author]
end
编辑:我还使用流派模型上的:association_foreign_key 选项使其工作
class Genre < ActiveRecord::Base
has_and_belongs_to_many :books, association_foreign_key: [:title, :author]
end
【问题讨论】:
标签: ruby-on-rails