Rails has_many 通过:(多对多关系)错误未初始化常量Group::Keywords
Rails has_many through: (many to many relationship) error uninitialized constant Group::Keywords
一个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
尝试更改以下内容
class GroupsKeyword < ActiveRecord::Base
belongs_to :groups
belongs_to :keywords
end
至
class GroupsKeyword < ActiveRecord::Base
belongs_to :group
belongs_to :keyword
end
一个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
尝试更改以下内容
class GroupsKeyword < ActiveRecord::Base
belongs_to :groups
belongs_to :keywords
end
至
class GroupsKeyword < ActiveRecord::Base
belongs_to :group
belongs_to :keyword
end