cancan gemでユーザーの能力問題を解決しようとしています。会社とユーザーは、会社に多くのユーザーがいて、ユーザーが多くの会社に属しているように、user_company_assignment を介して関連付けられています。会社の表示アクションを、会社に関連付けられているユーザーのみに制限したいと思います。以下に、2 つのモデルのコードと、initialize ロールの継承と、seller ユーザーのメソッドを含むability.rb の抜粋がありますが、これは機能せず、常に会社の詳細が表示されます。
会社.rb
has_many :user_company_assignments
has_many :user, :through => :user_company_assignments
ユーザー.rb
has_many :user_company_assignments
has_many :companies, :through => :user_company_assignments
アビリティ.rb
def initialize(user)
@user = user || User.new # for guest
@user.roles.each { |role| send(role.name.downcase) }
end
def seller
can :manage, :all
cannot :destroy, :all
can :show, Company do |company|
company.user_ids.include? @user.id
end
end