ryanb/cancan — Issue #522: Manage not removing a join filter
Author: jgeorge300
Created: 2011-11-30 19:23:21+00:00
State: open

I have a 'User' and 'Program' models with a has_and_belongs_to_many with each other. For non admin users, I only want them to read those Programs they have been assigned to. Admins should be able to manage any program. The non-admin usage appears to work correctly, but the admin does not. When the admin accesses the index (or fetch all records), they receive duplicate entries.

I have the following abilities configured:

```ruby
if user.has_role? :staff
can :read, Program, :users => {:id => user.id}
end

if user.has_role? :superadmin
can :manage, :all
can :access, :rails_admin
end
```

From the console, I see the following sql when fetching programs for non-admins:

```sql
SELECT "programs".* FROM "programs"
INNER JOIN "programs_users" ON "programs_users"."program_id" = "programs"."id"
INNER JOIN "users" ON "users"."id" = "programs_users"."user_id"
WHERE "users"."id" = 4
```
For admins:

```sql
SELECT "programs".* FROM "programs"
INNER JOIN "programs_users" ON "programs_users"."program_id" = "programs"."id"
INNER JOIN "users" ON "users"."id" = "programs_users"."user_id"
WHERE ('t'='t')
```
It appears that the :manage clears the where filter but not the join. Since it is a many-to-many relationship, I get a program for each user associated with it

How should the join be cleared or how do I get a distinct resultset?