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] )

On Sunday, February 16, 2014 10:57:33 PM UTC-6, Роман Ярыгин wrote:

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?

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

On Sunday, February 16, 2014 10:57:33 PM UTC-6, Роман Ярыгин wrote:

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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs