无法修改关联 'User#roles',因为源反射 class 'Role' 通过 :has_many 关联到 'Profile'
Cannot modify association 'User#roles' because the source reflection class 'Role' is associated to 'Profile' via :has_many
我有三个模型,一个用户,一个配置文件和一个角色,我正在尝试将多个 角色 附加到 user 通过 个人资料.
# user.rb
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
has_many :roles, through: :profile
has_many :interests, through: :profile
has_many :industries, through: :profile
end
# profile.rb
class Profile < ActiveRecord::Base
has_one :user, dependent: :destroy
has_many :roles
has_many :interests
has_many :industries
end
# role.rb
class Role < ActiveRecord::Base
has_many :profiles
has_many :users, :through => :profiles
has_many :users
end
我正在尝试在 edit.html.erb (simple_form_for)
中使用以下内容
<%= f.association :roles,
as: :check_boxes,
label_method: :name,
value_method: :id,
label: 'Roles' %>
使用以下参数
params.require(:user).permit([:email, :first_name, :last_name, :admin, :password, role_ids: [] ])
我的edit/update方法:
# GET /users/1/edit
def edit
@user.build_profile if @user.profile.nil?
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
给我以下错误信息
Cannot modify association 'User#roles' because the source reflection
class 'Role' is associated to 'Profile' via :has_many.
我已经研究了这个问题好几个小时了,但我似乎找不到错误。
您定义关系的方式有几个问题。
has_one
将外键放在关系的反面。因此,如果您在两端声明与 has_one
的关系,ActiveRecord 无法加载该关联。相反,您需要在带有外键的一侧使用 belongs_to
:
# user.rb
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
end
# profile.rb
class Profile < ActiveRecord::Base
belongs_to :user, dependent: :destroy
end
当涉及到用户和角色时,您在双方都声明了 has_many,但没有告诉 ActiveRecord 在哪里存储关系。在双向多对多关系中,您需要将 has_many through:
与连接模型一起使用或 has_and_belongs_to_many
.
通常在构建基于角色的访问系统时,您会希望定义关系,以便角色 belongs_to
是单个用户,而不是具有单个 Admin
角色,例如多个用户。这允许您将角色限定在资源范围内,并使添加/删除角色的逻辑变得不那么复杂(您正在添加(或删除)一个角色 to/from 一个用户,而不是从一个角色中添加或删除一个用户。)
如果您正在验证/授权您不想使用的用户模型 has_many :roles, through: :profiles
- 三方连接会慢得多,并增加不必要的复杂性和间接性。
如果您确实需要 "profiles" table,则将其用于有关用户的辅助信息,并在需要时加入。
class User < ActiveRecord::Base
has_many :roles,
has_one :profile, dependent: :destroy
end
# Columns:
# - user_id [integer, index, foreign key]
# - resource_id [integer, index]
# - resource_type [string, index]
class Role < ActiveRecord::Base
belongs_to :user, dependent: :destroy
belongs_to :resource, polymorphic: true,
dependent: :destroy
end
# Columns:
# - user_id [integer, index, foreign key]
class Profile < ActiveRecord::Base
belongs_to :user
# this is option but can be useful
has_many :roles, through: :user
end
我真的建议你从阅读官方开始rails guides article on associations。
我有三个模型,一个用户,一个配置文件和一个角色,我正在尝试将多个 角色 附加到 user 通过 个人资料.
# user.rb
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
has_many :roles, through: :profile
has_many :interests, through: :profile
has_many :industries, through: :profile
end
# profile.rb
class Profile < ActiveRecord::Base
has_one :user, dependent: :destroy
has_many :roles
has_many :interests
has_many :industries
end
# role.rb
class Role < ActiveRecord::Base
has_many :profiles
has_many :users, :through => :profiles
has_many :users
end
我正在尝试在 edit.html.erb (simple_form_for)
中使用以下内容<%= f.association :roles,
as: :check_boxes,
label_method: :name,
value_method: :id,
label: 'Roles' %>
使用以下参数
params.require(:user).permit([:email, :first_name, :last_name, :admin, :password, role_ids: [] ])
我的edit/update方法:
# GET /users/1/edit
def edit
@user.build_profile if @user.profile.nil?
end
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
给我以下错误信息
Cannot modify association 'User#roles' because the source reflection class 'Role' is associated to 'Profile' via :has_many.
我已经研究了这个问题好几个小时了,但我似乎找不到错误。
您定义关系的方式有几个问题。
has_one
将外键放在关系的反面。因此,如果您在两端声明与 has_one
的关系,ActiveRecord 无法加载该关联。相反,您需要在带有外键的一侧使用 belongs_to
:
# user.rb
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
end
# profile.rb
class Profile < ActiveRecord::Base
belongs_to :user, dependent: :destroy
end
当涉及到用户和角色时,您在双方都声明了 has_many,但没有告诉 ActiveRecord 在哪里存储关系。在双向多对多关系中,您需要将 has_many through:
与连接模型一起使用或 has_and_belongs_to_many
.
通常在构建基于角色的访问系统时,您会希望定义关系,以便角色 belongs_to
是单个用户,而不是具有单个 Admin
角色,例如多个用户。这允许您将角色限定在资源范围内,并使添加/删除角色的逻辑变得不那么复杂(您正在添加(或删除)一个角色 to/from 一个用户,而不是从一个角色中添加或删除一个用户。)
如果您正在验证/授权您不想使用的用户模型 has_many :roles, through: :profiles
- 三方连接会慢得多,并增加不必要的复杂性和间接性。
如果您确实需要 "profiles" table,则将其用于有关用户的辅助信息,并在需要时加入。
class User < ActiveRecord::Base
has_many :roles,
has_one :profile, dependent: :destroy
end
# Columns:
# - user_id [integer, index, foreign key]
# - resource_id [integer, index]
# - resource_type [string, index]
class Role < ActiveRecord::Base
belongs_to :user, dependent: :destroy
belongs_to :resource, polymorphic: true,
dependent: :destroy
end
# Columns:
# - user_id [integer, index, foreign key]
class Profile < ActiveRecord::Base
belongs_to :user
# this is option but can be useful
has_many :roles, through: :user
end
我真的建议你从阅读官方开始rails guides article on associations。