ryanb / cancan

Authorization Gem for Ruby on Rails.
MIT License
6.28k stars 785 forks source link

Mongoid: instances loaded with .only() never satisfy can? method #793

Open sbauch opened 11 years ago

sbauch commented 11 years ago

Using rails 3.2.6, CanCan 1.6.8, and mongoid 3.05.

I have a Products collection where each Product document can be quite large.

Each user document has an array of Product IDs that the user "owns" under the :products field, and the user can manage those products.

Defined in ability.rb as

can :manage, Product, :_id.in => user.products 

I want to load all of my products in an index action, and use the can? method in the view to either render a link to the show action of the product if the current_user owns it or simply the name of the product if the user does not.

I want to use the mongo .only() query modifier to return only those fields that are immediately necessary. I thought that including the id would allow the can? method to work properly, but it does not.

def index
  @products = Product.all #can? in view satisfied for proper products
  @products = Product.all.only(:name, :_id, :price)  # can? never satisfied
end

Does CanCan require I load the whole mongo document? I'd be happy to post more of my application code, but since it all works flawlessly without .only() I have to believe this is an isolated issue.

carloslopes commented 11 years ago

@sbauch Is this still an issue?

xhoy commented 10 years ago

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!