Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Multiple Conditions with scopes #566

Open
the8472 opened this Issue Mar 1, 2012 · 4 comments

Comments

Projects
None yet
3 participants

the8472 commented Mar 1, 2012

The Defining Abilities wiki page states the following about using scopes:

You cannot use this with multiple can definitions that match the same action and model since it is not possible to combine them.

I would like to question this supposed impossibility.

Here is a quick proof of concept (read: hack) that can merge Hash and ActiveRecord::Relation conditions using arel 2.0.10 and cancan 1.6.7. It currently does not support negative abilities - which could be remedied by making the reduce phase a bit smarter - and always kicks in, even when no scopes are present.

All joins used in the scopes and hashes are also converted to outer joins since different joins may be necessary for different where conditions. This might have unintended side-effects and require an additional distinct/group by clause.

https://gist.github.com/1952018

the8472 commented Apr 13, 2012

Updated the patch with some improved, cleaner logic that should handle negated scopes properly.

Collaborator

derekprior commented May 11, 2012

Hmmm, I don't see reference to this on the defining abilities wiki. A google search found it here, however:
https://github.com/ryanb/cancan/wiki/defining-abilities-with-blocks

I'm going to tag this as a feature request and for discussion. I have not tried your gist, but I did peruse it. To me it seems to be too down-in-the-weeds for inclusion in a gem like cancan, but that's not ultimately up to me.

the8472 commented May 12, 2012

To me it seems to be too down-in-the-weeds

I assume you're talking about accessing the arel underlying the scopes. It's not too different from what rails is doing internally, apart from the converting inner to outer joins, which is an issue with rails not supporting outer joins properly.

Outer joins are necessary for complex queries and in fact cause issues for cancan in other places too, see #374 (and a few others) for example.

@elabs-dev elabs-dev pushed a commit to elabs/cancan that referenced this issue Aug 22, 2012

Jonas Nicklas and Nicklas Ramhöj Allow multiple abilities with associations
There are two issues with the current way cancan handles associations:

1) Records are returned multiple times in some circumstances
2) Several defined abilities prevent some records to show up under certain circumstances

This commit includes tests for both cases. It fixes both problems by changing `joins` to `includes` for the AR adapters. This could have performance implications, since `includes` will also select all columns in the associated records. We tried various ways of achieving the same thing using Arel directly, but were unable to make this work due to lack of support for outer joins in Rails 3.1.

This closes issues #724, #566 and #613
2ffdf46

@bryanrite bryanrite added a commit to bryanrite/cancan that referenced this issue Jan 27, 2014

@bryanrite Jonas Nicklas and Nicklas Ramhöj + bryanrite Allow multiple abilities with associations
There are two issues with the current way cancan handles associations:

1) Records are returned multiple times in some circumstances
2) Several defined abilities prevent some records to show up under certain circumstances

This commit includes tests for both cases. It fixes both problems by changing `joins` to `includes` for the AR adapters. This could have performance implications, since `includes` will also select all columns in the associated records. We tried various ways of achieving the same thing using Arel directly, but were unable to make this work due to lack of support for outer joins in Rails 3.1.

This closes issues #724, #566 and #613
8976293

xhoy commented Apr 10, 2014

Dear submitter, Since cancan/raynB hasn't been active for more than 6 months and no body else then ryam himself has commit permissions the cancan project is on a stand still.
Since cancan has several issues including missing support for rails 4 cancan is moving forward to cancancan. More details on: #994

If your feel that your pull request or bug is still applicable (and hasn't been merged in to cancan) it would be really appreciated if you would resubmit it to cancancan (https://github.com/cancancommunity/cancancan)

We hope to see you on the other side!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment