Rails:找不到协会;也许你拼错了吗?

时间:2013-04-26 16:27:30

标签: ruby-on-rails activerecord jointable

我有一个与User模型有has_many :through关系的Publication模型。 Publication模型与has_many :through的关系Author

class User < ActiveRecord::Base
  has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
  has_many :publications, :through => :library_publications
end

class Library::Publication < ActiveRecord::Base
  belongs_to :publication
  belongs_to :user
end

class Publication < PublicationBase
  has_many :library_publications, :dependent => :destroy, :class_name => "Library::Publication"
  has_many :users, :through => :library_publications
  has_many :publication_contributions, :dependent => :destroy, :class_name => "Publication::Contribution"
  has_many :authors, :through => :publication_contributions
end

class Author < AuthorBase
  has_many :publication_contributions, :dependent => :destroy, :class_name => "Publication::Contribution"
  has_many :publications, :through => :publication_contributions
end

class Publication::Contribution < Publication::ContributionBase
  belongs_to :publication, :class_name => "Publication"
  belongs_to :author, :class_name => "Author"
end

据我所知,所有关联都写得正确。但是,当我尝试从用户那里收集作者时:

@user.library_publications.includes(:publication => [:authors])

我收到此错误:

Association named 'authors' was not found; perhaps you misspelled it?

这可能是什么原因?

1 个答案:

答案 0 :(得分:2)

经过一番实验,我发现所有publication的关联都被打破了。这导致我寻找更大的问题,最终我发现这个问题是由其中一个名为Library::Publication的join-table引起的。当我对其进行去命名时,publication的关联开始重新开始工作。

但是,我不确定为什么会这样。如果有人有解释,请分享。