Rails has_many通过:(多对多关系)错误未初始化的常量Group :: Keywords

时间:2015-06-30 11:05:28

标签: ruby-on-rails ruby-on-rails-3 associations has-many-through

Group可以包含多个关键字,Keyword可以包含多个关键字。我把这种关系定义为

class Keyword < ActiveRecord::Base
   has_many :groups_keywords
   has_many :groups, through: :groups_keywords
end

class GroupsKeyword < ActiveRecord::Base
  belongs_to :groups
  belongs_to :keywords
end

class Group < ActiveRecord::Base
  has_many :groups_keywords
  has_many :keywords, through: :groups_keywords
end

我可以做Group.find(1).groups_keywords所以关系正常吗?

但我希望获得所有Groups个关键字,以便执行以下操作。

Group.find(1).keywords

但我收到错误uninitialized constant Group::Keywords

1 个答案:

答案 0 :(得分:2)

尝试更改以下内容

class GroupsKeyword < ActiveRecord::Base
  belongs_to :groups
  belongs_to :keywords
end

class GroupsKeyword < ActiveRecord::Base
  belongs_to :group
  belongs_to :keyword
end