我正在编写关键字搜索算法。关键字存储在keywords
表/模型中,提交(进行搜索)存储在submissions
表/模型中。还有一个submission_keywords
表,根据(我非常确定的)submissions
设置,通过其唯一keywords
将ids
与has_many :through
相关联。但是,当我尝试在keywords
视图中显示submission
的{{1}}时:
Show
我收到错误:
<p>
<b>Keywords:</b>
<% @submission.keywords.each do |kw| %>
<%= kw.name %>
<% end %>
</p>
以下是我对这3个模型的代码:
Submission.rb:
ActiveRecord::HasManyThroughAssociationNotFoundError
Could not find the association :submission_keyword in model Submission
Keyword.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
Submission_Keyword.rb
class Keyword < ActiveRecord::Base
attr_accessible :name, :priority, :type
has_many :submission_keywords, :dependent => :destroy
has_many :submissions, :through => :submission_keyword
end
以下是在视图中class SubmissionKeyword < ActiveRecord::Base
attr_accessible :freq, :keyword_id, :submission_id, :weight
belongs_to :submission
belongs_to :keyword
end
请求keywords
的代码:
我看过很多帖子,人们忘记在submission
模型中添加has_many
关联,但正如您所看到的,我已经介绍过了。知道会出现什么问题吗?
答案 0 :(得分:2)
只是一个小错误。应该是:
has_many :keywords, :through => :submission_keywords
和
has_many :submissions, :through => :submission_keywords
注意submission_keyword * s *