简体   繁体   English

具有范围的活动管理员索引导致NoMemoryError:无法分配内存

[英]Active Admin index with scope results in NoMemoryError: failed to allocate memory

Working with Rails 4.2.6, I have this user model with a custom scope. 使用Rails 4.2.6时,我有一个带有自定义范围的用户模型。 Here is my models/user.rb: 这是我的模型/user.rb:

class User < ActiveRecord::Base
  scope :active_customers, -> { patient.where.not(customer_id: nil) }

I am trying to display an index of all the active_customers using Active Admin, here is my admin/customer.rb: 我正在尝试使用Active Admin显示所有active_customers的索引,这是我的admin / customer.rb:

ActiveAdmin.register User, as: "Customer" do
  menu priority: 8, label: proc{ I18n.t "admin.financial_report" }, parent: 'dev'
  actions :index

controller do
  def scoped_collection
    User.active_customers
  end
end

An it works locally and in staging but it crashes in production server. 它在本地和登台运行,但在生产服务器中崩溃。

I already have an index for all the patients that works perfectly in every enviroment. 我已经有了一个在所有环境下都能正常工作的所有患者的指数。 Here is the admin/patient.rb: 这是admin / Patient.rb:

ActiveAdmin.register User, as: "Patient" do
 menu priority: 1, label: proc{ I18n.t "admin.users" }
 permit_params [:nickname, :name, :surname, :email, :password, :password_confirmation, :therapist_id, :created_by_admin, :randword, :phone, :active]
 actions :all, except: [:destroy]

 controller do
   def scoped_collection
     User.patient
   end

Logs in production doesn't display any trace of the SQL query because it seems to break before: 生产日志不会显示SQL查询的任何痕迹,因为它似乎在以下时间之前就中断了:

Started GET "admin/customers"
Processing by Admin::CustomersController#index
Completed 500 Internal Server Error

After checking the result for rake routes I can see that there is no route for "admin/customers" but it doesn't breaks locally neither staging server. 在检查了rake路由的结果之后,我可以看到没有用于“ admin / customers”的路由,但是它并没有在本地中断,也没有登台服务器。 Any clue about why is it happening? 关于它为什么发生的任何线索?

In my routes.rb I have: 在我的routes.rb中,我有:

ActiveAdmin.routes(self)

In staging we have: 在分期中,我们有:

User.patient.count => 397
Benchmark.realtime { User.patient } => 0.0014501959958579391
User.active_customers.count => 99
Benchmark.realtime { User.active_customers } => 0.0016011869884096086

And in production: 并在生产中:

User.patient.count => 27853
Benchmark.realtime { User.patient } => 0.007111124003131408
User.active_customers.count => 496
Benchmark.realtime { User.active_customers } => 0.0040691940012038685

So, although the difference of numbers between enviroments I don't think that the reason for the memory error in production is the number of users. 因此,尽管环境之间数字的差异,但我认为生产中出现内存错误的原因不是用户数。

Also tried to use the scope that works in patient.rb in the customer.rb and it crashed: 还尝试在customer.rb中使用在Patient.rb中起作用的范围,它崩溃了:

controller do
  def scoped_collection
    User.patient
  end

After that I tried changing as:"Customer" to as:"User" and crashed again: 之后,我尝试将as:“ Customer”更改为as:“ User”,然后再次崩溃:

  ActiveAdmin.register User, as: "User" do

If the scope that works in patient.rb doesn't work in customer.rb it makes me discard the idea that the problem is caused just because a lack of memory. 如果在Patient.rb中起作用的范围在customer.rb中不起作用,这使我放弃了仅由于内存不足而引起问题的想法。 Pretty sure that the problem is related to the way I use the scope in the active admin or my syntax in customer.rb but I can't figure out how to fix it. 可以肯定的是,问题与我在活动管理员中使用作用域的方式或在customer.rb中使用语法有关,但我不知道如何解决它。

I would first check the SQL statements in your Rails log for anything unexpected. 首先,我将检查Rails日志中的SQL语句是否有意外情况。 It's not obvious to me what is wrong, but personally I would do the scoping at the ActiveRecord model level instead of ActiveAdmin, eg: 对我来说,错在哪里不是很明显,但是我个人会在ActiveRecord模型级别而不是ActiveAdmin上进行范围界定,例如:

class ActiveCustomer < User
  default_scope { where.not(customer_id: nil) }
end

ActiveAdmin.register ActiveCustomer do
  ...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM