[英]Active Admin index with scope results in NoMemoryError: failed to allocate memory
使用Rails 4.2.6時,我有一個帶有自定義范圍的用戶模型。 這是我的模型/user.rb:
class User < ActiveRecord::Base
scope :active_customers, -> { patient.where.not(customer_id: nil) }
我正在嘗試使用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
它在本地和登台運行,但在生產服務器中崩潰。
我已經有了一個在所有環境下都能正常工作的所有患者的指數。 這是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
生產日志不會顯示SQL查詢的任何痕跡,因為它似乎在以下時間之前就中斷了:
Started GET "admin/customers"
Processing by Admin::CustomersController#index
Completed 500 Internal Server Error
在檢查了rake路由的結果之后,我可以看到沒有用於“ admin / customers”的路由,但是它並沒有在本地中斷,也沒有登台服務器。 關於它為什么發生的任何線索?
在我的routes.rb中,我有:
ActiveAdmin.routes(self)
在分期中,我們有:
User.patient.count => 397
Benchmark.realtime { User.patient } => 0.0014501959958579391
User.active_customers.count => 99
Benchmark.realtime { User.active_customers } => 0.0016011869884096086
並在生產中:
User.patient.count => 27853
Benchmark.realtime { User.patient } => 0.007111124003131408
User.active_customers.count => 496
Benchmark.realtime { User.active_customers } => 0.0040691940012038685
因此,盡管環境之間數字的差異,但我認為生產中出現內存錯誤的原因不是用戶數。
還嘗試在customer.rb中使用在Patient.rb中起作用的范圍,它崩潰了:
controller do
def scoped_collection
User.patient
end
之后,我嘗試將as:“ Customer”更改為as:“ User”,然后再次崩潰:
ActiveAdmin.register User, as: "User" do
如果在Patient.rb中起作用的范圍在customer.rb中不起作用,這使我放棄了僅由於內存不足而引起問題的想法。 可以肯定的是,問題與我在活動管理員中使用作用域的方式或在customer.rb中使用語法有關,但我不知道如何解決它。
首先,我將檢查Rails日志中的SQL語句是否有意外情況。 對我來說,錯在哪里不是很明顯,但是我個人會在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.