私には協会があり
ます。著者にはたくさんの本があります。そして本には多くの著者がいます。
:throughオプションを使用する必要があります(「relations」という名前のテーブルを介して、「left_id」(author_idとして使用)および「right_id」(adbook_idとして使用)という名前の2つの列があります。
class Relation < ActiveRecord::Base
belongs_to :books
belongs_to :authors
end
class Author < ActiveRecord::Base
has_many :relations, :foreign_key => 'left_id'
has_many :books, :through => :relations
end
コンソールの場合:
> author = Author.new
> author.books
# => Error: no such column: relations.book_id
では、どうすれば「book_id」を「right_id」に指定できますか?(「foreign_key」のようなオプションはありますか?)