In a Rails application, I have two models like this :

class Painting < ActiveRecord::Base
  belongs_to :artist
end

class Artist < ActiveRecord::Base
  belongs_to :country

  def display_name
    text = to_s
    if birth_year
      death = death_year || "----"
      text += " (#{birth_year}-#{death})"
    end
    text += ", #{country.name}"
  end

end

class Country < ActiveRecord::Base
  active_admin_translates :name
end

I use active admin like this

ActiveAdmin.register Painting do
end

The problem is than the display_name method need to call countries and translations tables. There is a lot of artists and it's very long to run. I'm looking for a way to to increase the speed.

Request seems like this :

SELECT "artists".* FROM "artists" WHERE "artists"."accepted" = 't' ORDER BY name
SELECT "countries".* FROM "countries" WHERE "countries"."id" = 50 ORDER BY name LIMIT 1

All artists are requested to do this input : select image

What can I do?

有帮助吗?

解决方案

Have you tried setting the scoped_collection on the controller?

I think it's something like this:

ActiveAdmin.register Painting do
  controller do
    def scoped_collection
      Painting.joins({artist: {country: :translations}})
    end
  end
end

More info available here: http://www.activeadmin.info/docs/2-resource-customization.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top