leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
537 stars 215 forks source link

Eager loading of relation not supported? Wice Grid can only do N+1 ? #278

Closed jbwl closed 8 years ago

jbwl commented 8 years ago

On Wice Grid 3.5.0 with Rails 3, I am using the following ActiveRecord finder:

Thing.eager_load(:stuff)

to initialize the grid.

In the view, I am referencing the stuff columns like this:

t.stuff.stuff_attribute

This results in the stuff attribute pulled from the database for each row (N+1) - even though it has already been loaded in the AR call, and the performance is accordingly bad.

Am I referencing the stuff column in the wrong way? I have found no solution in the manual.

leikind commented 8 years ago

Can you try smth like

@tasks_grid = initialize_grid(Task,
  include:    :project,
  conditions: {archived: false, project: {active: true}}
)
jbwl commented 8 years ago

Thank you for the reply. Your code results in 2 queries (one for Thing, and one for all the Stuff objects with a "IN (A,B,C)" condition. Much better.

Just out of interest, is there a possibility to use a relation in Wice Grid and have a true "Eager Load" of related objects through a "LEFT OUTER JOIN" query? This is the default for an AR .eager_load(:stuff) call, but if I use this, I don't know how to reference stuff columns in the view without Wice Grid producing N+1 queries.

Thanks again.

leikind commented 8 years ago

Your code results in 2 queries

No, this is what ActiveRecord does with a corresponding AR call. The plugin does not use AR .eager_load internally

jbwl commented 8 years ago

Thanks. I would like to request the possibility to make Wice Grid use .eager_load internally then.

leikind commented 8 years ago

This plugin is looking for maintainers. I am not adding anything to it.