I have the following:
- User model that
has_and_belongs_to_many
Restaurants and vice-versa. - Restaurant model
that has_and_belongs_to_many
Meals and vice-versa.
In my ability.rb
file, I want to specify that a user can only manage meals of restaurants he "has_and_belongs_to" (i.e. restaurants in user.restaurants
).
I have this right now:
class Ability
include CanCan::Ability
def initialize(user)
# a user can only manage meals of restaurants he has access to
can :manage, Meal do |meal|
meal.restaurant_ids & user.restaurant_ids #...this doesn't work...
end
end
end
What's the best way to do this?
I've consulted https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks but I still don't know.