本文介绍了扶手:为什么"的has_many ...:通过=> ..."协会结果" NameError:未初始化不断..."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要前preSS,一个组可以有多个用户,并且用户可以属于多个组,我设置以下关联:
To express that a group can have multiple users, and a user can belong to multiple groups, I set the following associations:
class Group < ActiveRecord::Base
has_many :users_groups
has_many :users, :through => :users_groups
end
class User < ActiveRecord::Base
has_many :users_groups
has_many :groups, :through => :users_groups
end
class UsersGroups < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
然而,当我键入:
However, when I type:
Group.find(1).users
我得到:
NameError: uninitialized constant Group::UsersGroup
我在做什么错了?
What am I doing wrong ?
推荐答案
类UsersGroups
应类UsersGroup
。 ActiveRecord的模型是奇异 - 的表具有多个
class UsersGroups
should be class UsersGroup
. ActiveRecord models are singular - the tables are plural.
这篇关于扶手:为什么&QUOT;的has_many ...:通过=&GT; ...&QUOT;协会结果&QUOT; NameError:未初始化不断...&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!