load_resource with nested resource loads all records #398

gucki opened this Issue Jun 13, 2011 · 2 comments

3 participants


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?


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.


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.

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