Rails Admin Eager loading relations for list view
Asked Answered
R

1

6

We have a list view for a model Ticket in rails admin that loads very slowly.

class Ticket < ActiveRecord::Base
  belongs_to :crew
end

The reason it is slow is that we display the ticket's crew relation through a method rails_admin_pretty_print which accesses other related models.

class Crew < ActiveRecordBase
   belongs_to :pool
   belongs_to :leader

   def rails_admin_pretty_print
      "leader : #{leader.name} at time #{pool.datetime}"
   end
end

I want to eager load all of these objects in the initial query in order to speed up the request. Something like:

config.model "Ticket" do
   object_label_method :rails_admin_pretty_print
   list do
      field :crew, includes(:pool, :leader)
   end
end

I can't find any way to do this in the rails admin docs. Is there a way of doing this?

Rida answered 11/3, 2014 at 17:59 Comment(2)
Any better solution than one i answer?Harhay
any solution yet?Pembroke
I
-2

If the default_scope of a model is set, Rails Admin uses it for the list view. Obviously it's not ideal (unless this is incredibly important and worth the workaround), but you could set your default scope in your Crew model as follows:

class Crew < ActiveRecord::Base
  default_scope { includes(:pool, :leader) }

  # more crew stuff
end

Like I said, it's not ideal, because you'd have to use unscoped every time you want to access your Crew model without including :pool and leader.

Crew.unscoped.where(id: [1,2,4])
Irvinirvine answered 26/4, 2014 at 6:18 Comment(1)
Not ideal not even useful in most cases, because rails_admin is for ADMINS only, and there a lot of other users than admins. So, yeah not ideal. Anyway, thanks for the infoHarhay

© 2022 - 2024 — McMap. All rights reserved.