【发布时间】:2010-08-14 07:18:29
【问题描述】:
我有一个关联:
作者有很多书;
一本书有很多作者;
我需要使用 :through 选项(通过名为“relations”的表,有两列名为“left_id”(用作 author_id)和“right_id”(用于广告 book_id);
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' 的选项?)
【问题讨论】:
标签: ruby-on-rails ruby has-many-through associations