..." association results in "NameError: uninitialized constant ..."-6ren"> ..." association results in "NameError: uninitialized constant ..."-为了表达一个组可以有多个用户,一个用户可以属于多个组,我设置了以下关联: class Group :users_groups end class User :users_groups end cl-6ren">
gpt4 book ai didi

ruby-on-rails - rails : Why "has_many ..., :through => ..." association results in "NameError: uninitialized constant ..."

转载 作者:行者123 更新时间:2023-12-03 21:17:13 28 4
gpt4 key购买 nike

为了表达一个组可以有多个用户,一个用户可以属于多个组,我设置了以下关联:

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

但是,当我输入:
Group.find(1).users

我得到:
NameError: uninitialized constant Group::UsersGroup

我究竟做错了什么 ?

最佳答案

class UsersGroups应该是 class UsersGroup . ActiveRecord 模型是单数——表格是复数。

关于ruby-on-rails - rails : Why "has_many ..., :through => ..." association results in "NameError: uninitialized constant ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7040022/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com