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?

zimkies
  • 1,067
  • 1
  • 9
  • 20

1 Answers1

-1

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])
Joe Kennedy
  • 9,365
  • 7
  • 41
  • 55
  • 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 info – Zia Ul Rehman Mughal Oct 25 '18 at 10:15