Associations in conditions hash #374
Comments
You can pass in a scope but you'll need to define the behavior in Ruby as well. can :read, Photo, Photo.includes(:groups).where(:groups => {:id => nil}) do |photo|
photo.groups.empty?
end It may not be the cleanest, but does that work for you? |
Yes! Thank you. BTW, are there any reasons preventing from using |
The It is unfortunate that |
Upvote for outer joins! Very annoying to work around. |
And a more lightweight hack (which also requires squeel): https://gist.github.com/2312198 For Rails >= 3.1 only another approach is also possible: Generate the query as usual, extract the arel ast for the joins, convert them from inner to outer joins and then merge the joins back in. That wouldn't require squeel. |
Marking this a documentation bug. The workaround Ryan originally offered should be documented with the Scope moved to the class that it queries. I think Ryan's reasoning for not doing |
Documentation added (see Hash of Abilities): |
I ended up monkey patching cancan to run conditions in subqueries instead of building outer joins. |
…-issues-lint-unused-block-argument Fix rubocop Issues: Lint/UnusedBlockArgument
Hi,
I have
Photo
andGroup
model.Photo has_and_belongs_to_many :groups
.I need to allow user view photos which do not belong to any group, so I thought I would write
can :read, Photo, :groups => {:id => nil}
, but it usesjoins
on association. Therefore, resulting SQL looks likewhich obviously always returns empty set.
What I actually need is a scope
Photo.includes(:groups).where(:groups => {:id => nil})
, which producesand returns what expected. Is there a clean way to define such ability with cancan?
The text was updated successfully, but these errors were encountered: