0

I have a model that acts as a nested set (tree), like so:

class Position < ActiveRecord::Base
  attr_accessible :parent_id
  acts_as_nested_set  

  belongs_to :person
  belongs_to :parent, :class_name => 'Position', :foreign_key => :parent_id
  has_many :children, :class_name => 'Position', :foreign_key => :parent_id
end

My ability.rb file contains this:

can :read, Position, :id => #a list of position ids

The list of ids will change depending on another setting that is not important to this situation.

My question is, how do get @position.children to only return those positions that are authorized in the ability?

1 Answers1

0

Sounds like you want conditions on the has_many within a lambda so it evaluates at run time... Something like:

has_many :children, :class_name => 'Position', :foreign_key => :parent_id, :conditions => lambda {|child| can? :read child }

I haven't tested this (obviously) but this should get you closer.

To get can? available in a model, see: Access CanCan's `can?` method from a model

Community
  • 1
  • 1
CDub
  • 13,146
  • 4
  • 51
  • 68