cancan icon indicating copy to clipboard operation
cancan copied to clipboard

Is CanCan needlessly creating many Ruby objects when DB rules are involved?

Open fj opened this issue 12 years ago • 3 comments

When I write a rule that involves a scope, like

class Ability
  def initialize(user)
    can :frobble, Bucket, Bucket.frobbleable_by(user)
  end
end

it seems CanCan stores the results of calling the frobbleable_by scope, and turns each and every result into a Ruby object. That seems undesirable and creates a lot of GC stress. Would it be better if it did this instead?

  • Keep the query around, but don't execute it.
  • If you need to decide if a given Bucket is frobbleable_by, ask the backing store if that bucket is in this scope.

This would delay creating any unneeded objects until the moment when the Rule is actually needed. Thoughts?

fj avatar Aug 30 '11 18:08 fj

Can you try the most recent version to see if your issue has been resolved? This issue is tagged 2.0, so you'll want to use the master branch.

This is one of the oldest CanCan issues with no discussion. CanCan is struggling right now to implement support for rails 4, and the issue count is nearing 200. It would be a big help if we could close a few old issues and get the issue count down. Thanks!

jaredbeck avatar Jul 14 '13 16:07 jaredbeck

We are facing the same problem and after tracking it down we came up with this solution:

https://github.com/shobee/cancan/commit/731ad3439dce7c088df4ad8266c3bea56886c9d4

Unfortunately it's not good enough to make a pull request since we don't get the test suite running locally. Besides that it takes a more generic solution. But in our case it's working for now and maybe this is a good starting point to get around this issue.

shobee avatar Jul 31 '13 14:07 shobee

Thanks for your submission! The ryanb/cancan repository has been inactive since Sep 06, 2013. Since only Ryan himself has commit permissions, the CanCan project is on a standstill.

CanCan has many open issues, including missing support for Rails 4. To keep CanCan alive, an active fork exists at cancancommunity/cancancan. The new gem is cancancan. More info is available at #994.

If your pull request or issue is still applicable, it would be really appreciated if you resubmit it to CanCanCan.

We hope to see you on the other side!

xhoy avatar Jul 01 '14 07:07 xhoy