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
答案 0 :(得分:2)
尝试更改以下内容
class GroupsKeyword < ActiveRecord::Base
belongs_to :groups
belongs_to :keywords
end
到
class GroupsKeyword < ActiveRecord::Base
belongs_to :group
belongs_to :keyword
end