CanCan: How to define ability with some conditions

Hello everyone!

I have code:

if user.role == “topmanager”

can :read, ActiveAdmin::Page, :name => “Dashboard”

can :manage, Realty, agent: {agency_id: user.agency_id}

And I want to add another condition, can :manage, Realty, agent: nil

How to do it? Defining it two times gets error:

undefined method `reflect_on_association’ for Class:Class

How can I define multiple conditions for one ability?

So you want to say “The user can manage Realty if the agent has the right agency ID or if the agent is nil”?

If so, don’t use the hash version of the condition. Go ahead and spell it out.

You could also try agent: {agency_id: [nil, user.agency_id] )

You can use the ‘or’ & ‘and’ operators in the ability class.

can :manage, Realty do |realty|

realty.new_record? or

realty.agency_id == nil or

realty.agency.agents.include?(user)

 end