Skip to content

Stuck on avoiding N+1 with associations #727

Answered by coorasse
Signum asked this question in Q&A
Discussion options

You must be logged in to vote

Your issue is not related to cancancan specifically. You want to eager load an association with scope. I wrote about this some time ago: https://medium.com/@coorasse/eager-load-associations-with-parameters-in-rails-6e9fd7b65923

I expect something like:

packages = Package.where(...)
ActiveRecord::Associations::Preloader.new.preload(packages, : screenshots, Screenshot.accessible_by(...))

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Signum
Comment options

@coorasse
Comment options

@Signum
Comment options

Answer selected by coorasse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants