stffn / declarative_authorization

An unmaintained authorization plugin for Rails. Please fork to support current versions of Rails
MIT License
1.24k stars 233 forks source link

use group by in results returned by with_permissions_to #158

Open dgm opened 11 years ago

dgm commented 11 years ago

Given a rule such as:

@@@ has_permission_on :children, :to => :read do if_attribute :enrollments => {:session => {:yearly_site_funding => {:includes_special_funding => true}}} end @@@

Calling Child.with_permissions_to(:read) returns back multiple duplicates of children, since they can have multiple enrollments. In SQL, if I could tack on a 'group by "children.id"', it comes back without dups.

Granted, I'm still stuck on some older versions of software: DA is (0.5.2) , rails 2.3. Has this been addressed at all yet, or is there an easy solution?

stffn commented 11 years ago

I'm hesitant in adding such a bold thing as group_by to every query. Do we really know what kind of group by is needed? If we cannot be entirely sure, I'd rather have the app dev add it.

dgm commented 10 years ago

Another solution to this problem would be to use the results of with_permissions_to as a subselect for another query, which would keep the permissions part separate from the application domain part.

dgm commented 10 years ago

As another example that works:

Enrollment.where(child_id: 19447, id: Enrollment.with_permissions_to(:read))

It's a little clunky, but this does yield the correct results.