has_many ...:尽管正确建模,协会仍然没有工作

时间:2013-06-06 14:22:47

标签: ruby-on-rails activerecord associations

我正在编写关键字搜索算法。关键字存储在keywords表/模型中,提交(进行搜索)存储在submissions表/模型中。还有一个submission_keywords表,根据(我非常确定的)submissions设置,通过其唯一keywordsidshas_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关联,但正如您所看到的,我已经介绍过了。知道会出现什么问题吗?

1 个答案:

答案 0 :(得分:2)

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

has_many :keywords, :through => :submission_keywords

has_many :submissions, :through => :submission_keywords

注意submission_keyword * s *