0

我正在写一个关键字搜索算法。关键字存储在keywords表/模型中,提交(发生搜索的地方)存储在submissions表/模型中。还有一个submission_keywords表格可以submissions根据(我很确定)设置keywords通过它们的独特链接。但是,当我尝试在我的视图中显示for a时:idshas_many :throughkeywordssubmissionShow

<p>
  <b>Keywords:</b>
  <% @submission.keywords.each do |kw| %>
  <%= kw.name %>
  <% end %>
</p>

我收到一个错误:

ActiveRecord::HasManyThroughAssociationNotFoundError
Could not find the association :submission_keyword in model Submission

这是我对这 3 个模型的代码:

提交.rb:

class Submission < ActiveRecord::Base
  attr_accessible :count, :is_sent, :is_success, :stamp, :url
  has_many :submission_keywords, :dependent => :destroy
  has_many :keywords, :through => :submission_keyword
end

关键字.rb:

class Keyword < ActiveRecord::Base
  attr_accessible :name, :priority, :type
  has_many :submission_keywords, :dependent => :destroy
  has_many :submissions, :through => :submission_keyword
end

Submission_Keyword.rb

class SubmissionKeyword < ActiveRecord::Base
  attr_accessible :freq, :keyword_id, :submission_id, :weight
  belongs_to :submission
  belongs_to :keyword
end

这是在视图中请求keywordsa的代码:submission

我看过很多帖子,人们忘记包含has_many:through模型的关联,但正如你所见,我已经涵盖了这一点。知道可能出了什么问题吗?

4

1 回答 1

2

只是一个小错误。它应该是:

has_many :keywords, :through => :submission_keywords

has_many :submissions, :through => :submission_keywords

通知 submit_keyword* s *

于 2013-06-06T14:34:08.610 回答