我有简单的书籍作者 HABTM 模型:
书籍 -> books_authors -> 作者
我有一个作者 ID + 标题字符串数组,需要查找是否已经有一本书具有该标题并且具有这些给定的作者。
有什么想法可以在 Rails 3 中使用 ActiveRecord 来做到这一点吗?
最佳答案
实现 habtm 的最佳实践现在是 has_many :though
models/Book.rb
class Book < < ActiveRecord::Base
has_many :authors
has_many :authorships, :through => author
models/Authorship.rb
class Authorship < ActiveRecord::Base
belongs_to :book
belongs_to :author
models/Author.rb
class Author < ActiveRecord::Base
has_many :authorships
has_many :authors, :through => :authorships
关于ruby-on-rails - ActiveRecord 通过 HABTM 引用查找记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9535261/