0

I want to use the ability.rb file of the cancancan gem to do record level authorization.

Let's say I have a User, which has_many Cars, which has_many Wheel.

I want to do authorization for Wheels, so that only Users which own the Car that owns the Wheel can manage it.

What is the best practice way of doing this?

kaizenx
  • 385
  • 1
  • 5
  • 15

1 Answers1

0

You can define it like this in your ability.rb

can :manage, Wheel, car: { user_id: user.id }

For more information see:

https://github.com/CanCanCommunity/cancancan/wiki/defining-abilities

davidwessman
  • 1,170
  • 8
  • 27
  • In some cases you also need to define block for rule. For example in activeadmin it was reading all records i have access to :read, but when can? :update, Article it was just not working. After i added block condition it solved issue. – Fedcomp Dec 11 '15 at 10:27