-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No way to use includes in ActiveRecord to avoid n+1 queries in views? #37
Comments
+1 something to avoid the n+1 queries would be great! |
+1 |
1 similar comment
+1 |
I believe the include statement you're looking for would be something like: |
That still results in n+1 queries.
|
Are you guys running v1.0.8 of the gem? (released Jan 27 2016) A change in commit 95e66e7 may have fixed this issue. (I was having similar issues that are now resolved on the latest version) |
We are using 1.0.2. I'll try to upgrade and see what it does. |
Another option is to use the standard Rails ActiveRecord relation helpers on your top-level acts_as model, with scopes around the actable_type field. All the class Store < ActiveRecord::Base
has_many :products
end
class Product < ActiveRecord::Base
actable
belongs_to :store
belongs_to :pen,
-> { where actable_type: 'Pen' },
foreign_key: "actable_id"
belongs_to :book,
-> { where actable_type: 'Book' },
foreign_key: "actable_id"
end
class Pen < ActiveRecord::Base
acts_as :product
has_one :product,
-> { where actable_type: 'Pen' },
foreign_key: "actable_id"
end
class Book < ActiveRecord::Base
acts_as :product
has_one :product,
-> { where actable_type: 'Book' },
foreign_key: "actable_id"
end Just watch out for We added this kind of code in a project that uses this Gem around the same time we updated to 1.0.8 and have been able to get efficient queries working most of the time now. It's a bit of a workaround, but until the gem itself has proper (It may not even be possible to add proper support for |
I had tried something like what you have above but when I use .specific on the product (actable) it gives a column error when trying to query for the acts_as. |
It doesn't seem there is a way to avoid n+1 queries when using acts_as:
Note it's not preloading Book or Pen.
If you're showing an index page that uses data from "specific" (or the actual classes that are acts_as :product) it generates additional queries, and there is no way to seemingly avoid this.
For example you might have workflow states that in the Pen class are defined as "full", "half-full", "empty"; and in Book "on_shelf", "on_loan" ... so you have different workflow_actions specific to their type. So you could imagine an index view where you want to show the status of each product and so having to call
product.specific.workflow_state
on each one results in a query (which is bad).Is there something I'm missing? You should be able to preload everything so there are a minimum set of queries (e.g. pull the store, all products, and all related pens and books). But you cannot really say
includes(products: [:specific])
or evenincludes(products: [:book, :pen])
(assuming you knew all the actable classes for product) because Product doesn't have any real association to Book or Pen.Is there some other strategy around dealing with this, which I'd imagine is a common use case?
The text was updated successfully, but these errors were encountered: