0

我希望有人已经经历过这一点。请帮助我,我该如何解决这个问题:

class Article < ActiveRecord::Base
  belongs_to :author
  belongs_to :publisher
  has_one :address, :through => :publisher
end

class Author < ActiveRecord::Base
  has_many :articles
  has_many :addresses, :through => :articles, :source => :address
end

我尝试获取“作者”的“地址”,但在控制台中出现此错误:

ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_one :through for has_many :addresses, :through => :articles.  Use :source to specify the source reflection.

author.articles[0].address工作正常。

我希望你给我建议,我该如何解决它。谢谢。

4

2 回答 2

0

This solution also worked well for different relation types.

e.g. User.registrations.join_table.periods

but you -can't- apply active_record methods on what is mapped.

e.g. user.periods(:order => :date) e.g. user.periods.model etc..

thanks

于 2010-08-05T18:42:45.093 回答
0

AR 不喜欢has_many通过has_one. 但是您可以使用以下方法轻松获取所有地址Author

def addresses
  articles.map {|article| article.address }
end
于 2009-11-07T16:42:15.613 回答