我的应用程序有一个用户模型、角色模型和分配模型。通过分配模型,用户和角色模型之间存在多对多关系。我想检查当前用户是否有特定的 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/