Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

load_resource with nested resource loads all records #398

Open
gucki opened this issue Jun 13, 2011 · 2 comments
Open

load_resource with nested resource loads all records #398

gucki opened this issue Jun 13, 2011 · 2 comments
Labels

Comments

@gucki
Copy link

gucki commented Jun 13, 2011

Using rails 3.1.0.rc4 and cancan 1.6.5. My controller contains a simple

load_resource :customer, :through => :current_account

When I now visit the index action (has a raise "test" in it so it does nothing), I see in my rails sql log:

User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Account Load (0.2ms)  SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT 1  [["id", "17"]]
Customer Load (27.0ms)  SELECT "customers".* FROM "customers" WHERE "customers"."account_id" = 17

As you can see the it loads all customers of the current account, which is really bad for performance. Shouldn't it by default load anything but only create a scope and store this in @customers?

@ryanb
Copy link
Owner

ryanb commented Jun 13, 2011

Hmm, it might need to do current_account.customers.scoped to ensure it's a scope and not load the records directly. Thanks for reporting.

@inkstak
Copy link

inkstak commented Dec 19, 2013

I fixed it in #974.

It comes from a .respond_to?(:accessible_by) called on the collection.
As @ryanb said, we might need to do a scoped, but only with ActiveRecord 3. This is not relevant with AR 4, which have remove the `scoped' method.

yan13to pushed a commit to paupauorg/cancan that referenced this issue May 30, 2017
MongoidAdapter: Improves querying for finding relation_ids by replacing only->map with distinct
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants