gpt4 book ai didi

ruby-on-rails - rails 4 : Conditional check set of values returned via many to many association

转载 作者:数据小太阳 更新时间:2023-10-29 08:31:54 30 4
gpt4 key购买 nike

我的应用程序有一个用户模型、角色模型和分配模型。通过分配模型,用户和角色模型之间存在多对多关系。我想检查当前用户是否有特定的 role_id。这有办法吗?

我的模型类如下

class User < ActiveRecord::Base
has_many :assignments
has_many :roles, :through => :assignments
end

class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
end

class Role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
end

最佳答案

current_user.role_ids.include?(role_id)

关于ruby-on-rails - rails 4 : Conditional check set of values returned via many to many association,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153151/

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