1

I have a routes.rb that looks like this:

resources :restaurants, :shallow => true do
  resources :orders do
    resources :foods
  end
  resources :categories do
    resources :foods
  end
end

something like this in my ability.rb works,

if user.role? :owner
  can :manage, Category, :restaurant => {:user_id => user.id}
  ...

but deeper nesting appears to be a problem with shallow nesting.

  can :manage, Food, :category => {:restaurant => {:user_id => user.id}}
end

Any idea on how to get CanCan to handle nesting that is as deep as the last example?

patrickdet
  • 91
  • 2
  • 10

1 Answers1

1

I don't think it's possible. You will have to do it yourself using a block:
https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks

can :manage, Food do |food|
    food.categories.joins(:restaurant).where("restaurants.user_id = ?", user.id).any?
end

What do you want exactly? Users can only manage food if it belongs to a restaurant owned by the user though categories?

Robin
  • 21,667
  • 10
  • 62
  • 85
  • yes, that is exactly what i want! i will look into abilities with blocks then. but you think it is possible with that or do i have to make some changes to my model? – patrickdet Jan 02 '12 at 19:35
  • yeah that works somehow. thank you. eventhough i switched to declaratice_authorization because nesting isn't that big of a hassle with that gem. thank you anyway. – patrickdet Jan 03 '12 at 21:21