Display has to many association activeadmin rails

2019-09-01 16:31发布

问题:

I have users which can have many identities, (depending on whether they login using facebook, google, or my site). In ActiveAdmin to display the identities I had the following code.

ActiveAdmin.register User do
menu label: 'Subscribers'
ActiveAdmin.register Identity do
belongs_to :user
 end 


index do
selectable_column
column :email
column :current_sign_in_at
column :sign_in_count
column :created_at
column "Sign-up Provider", :identities do |user|
      ul do

         user.identities.collect  do |identity|
             li do 
                 if identity.provider == "gplus"
                   "Google"
                 else identity.provider == "facebook"
                  identity.provider.capitalize
                 end
            end   
          end
         "Self"
       end
 end
actions
end

form do |f|
  f.inputs 'User Details' do
  f.input :email
  f.input :password
  f.input :password_confirmation
  end
  f.actions
 end
end

It works fine in the local host, and a remote development environment. But gives 502 error when run on staging which uses the production environment. seeing the log it seems the query takes long time. Trying to figure out why does it take longer time, it appeared as if the query to fetch the identity is executed for each user again and again, which is kind of an inefficient way. We also tried using the data at the staging and importing it in the local and development enviroment to see if it is the data size that is causing error, but it still works there. I understand the query is inefficient. What is the correct way to achieve this?

And if what I have used is the correct way to go for it, please help me resolve the error. Thanks.

Surprisingly, this error does not occur if i remove the identities column.

回答1:

Well, this problem had quite unusual solution, probably not specifically related to this problem but yes

The problem was with memory. On the server, the disk space became over-exceedingly used. We freed some memory by deleting some old log files, and also, used pluck instead of collect, just to fetch only the needed "provider" instead of the whole record.