Reputation: 109
I have problem with defining abilites when resources are deeply nested. I have these classes: Teacher, Division, Student, Absence and User (Teacher and Student belongs to Devise User model):
#Teacher
has_many :divisions
#Division
belongs_to :teacher
#Student
belongs_to :division
has_many :absences
#Absence
belongs_to :student
There is no problem when I want to ensure that Teacher can manage only Students that belongs to his division:
#This works
if user.teacher?
can :manage, Student, division: { teacher_id: user.teacher.id }
end
Problem occurrs when I want to ensure that Teacher can manage Absences that belongs to Students from his divisions:
#This doesn't work and returns PG::Error: ERROR: column students.divisions does not exist
can :manage, Absence, student: { division: { teacher_id: user.teacher.id } }
Any suggestions for defining ability for this nested resources?
Upvotes: 1
Views: 278
Reputation: 36
This should work:
if user.teacher?
can :manage, Absence do |absence|
absence.student.division.teacher_id == user.teacher.id
end
end
The cancan wiki: Defining Abilities with Blocks
Upvotes: 2