LoginSignup
0
0

More than 1 year has passed since last update.

Ruby | cancan でコンディションを与えた後に can? と聞く方法

Last updated at Posted at 2017-09-07

検証

初期状態では何も権限を与えていないので false が返る

can? :read, SomeClass
# => false

condition を指定して id: 1 に対して権限を与えてやる

can :read, SomeClass, id: 1

SomeClass クラスへの問いかけで true が返るようになる

can? :read, SomeClass
# => true

理由はこちらを参照: https://github.com/ryanb/cancan/wiki/Checking-Abilities

だが can と同じ文法ではコンディションを指定して can? 出来ない ( condition指定は無視されている様子 )

can? :read, SomeClass, id: 1
# => true
can? :read, SomeClass, id: 2
# => true

condition に対して can? と聞くには 、SomeClass のインスタンスを与えてやる

id: 1 でだけ true が返るのが分かる。

can? :read, SomeClass.new(id: 1)
# => true
can? :read, SomeClass.new(id: 2)
# => false

環境

  • cancancan (1.15.0)

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0