Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

The accessible_by call cannot be used with a block 'can' definition #957

Open
phlegx opened this Issue · 4 comments

4 participants

@phlegx

Using active_admin together with cancan results in an error:

The accessible_by call cannot be used with a block 'can' definition

My block description does look like the following:

can :manage, Authentication do |auth|
  auth.user_id == user.id || user.organization.include?(auth.organization)
end

It seems that cancan doesn't like accessible_by calls when the cancan definition is written in a block. Any ideas what I can do about this? Is it possible to write the block above in a non-block fashion? If yes, how?

cu

@graywh

Can you construct a hash of conditions for where to select the appropriate records? If so, use that instead.

If you cannot, you will need to pass an SQL fragment.

https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks

@phlegx

@graywh thanks! Could you give me an example of how to use a hash of conditions for "where" with a non-block statement to select the appropriate records?

I'm trying to figure out how to construct that.

thank you!

@cue232s

@phlegx did you solve this issue? If so, can you share you solution? Thanks.

@twessler

You can do it, but you will have to split it into two statements. Rails doesn't do OR with hashed conditions, but cancan puts abilities together with OR. The following should work:

def initialize(user)
  user ||= User.new

  can :manage,
    Authentication,
    :user_id => user.id

 can :manage,
   Authentication,
   :organization_id => user.organization_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.