Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

abilities with hash condition #975

Open
amrnt opened this issue Dec 14, 2013 · 2 comments
Open

abilities with hash condition #975

amrnt opened this issue Dec 14, 2013 · 2 comments

Comments

@amrnt
Copy link

amrnt commented Dec 14, 2013

Here's my models

in user model

has_many :clients, through: :memberships

in client model

has_many :memberships, dependent: :destroy
has_many :admins, through: :memberships, source: :user
has_many :managers, ->{ where ['memberships.role_cd = ?', Membership.manager] }, through: :memberships, source: :user

membership model

  belongs_to :client
  belongs_to :user

my ability definition (for normal users)

can :manage, User, id: user.id

can :create, Client
can :read, Client, admins: { id: user.id }
can [:update, :destroy], Client, managers: { id: user.id }

can :manage, Membership, client_application: { managers: { id: user.id } }

I'm using cancan with active_admin. It doesn't work as expected.

when I go to [admin]/clients, I got this exception: undefined method id' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_User:0x007fc808c9cfc0>

digging inside the code, on https://github.com/ryanb/cancan/blob/master/lib/cancan/rule.rb#L112

attribute = subject.send(name)

subject is an array of User records and name is the :id

Am I doing something wrong? or there's a bug?

@zamith
Copy link

zamith commented Mar 25, 2014

@amrnt For some reason you're not getting a single user, but a collection of users. Just from this code I cannot tell you why.

The quick fix would be to rename user to users and doing users.first.id. You might want to dig deeper and try to understand why you're getting a collection, though.

PS: ActiveAdmin works reasonably well with CanCan, so if it is due to the integration of the two, it's probably something you're doing wrong.

@tomdunning
Copy link

related to this: #917

switch to https://github.com/CanCanCommunity/cancancan - community supported version until RyanB returns

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants