Ruby on rails Rails作为json的性能问题
我有一组活动模型记录。阵列中最多有75个对象。然而,该模型具有多个关联和关系。现在我正在重写as_json方法,以便只包含必要的字段。加载60个对象大约需要13秒。我怎样才能加快速度?我尝试过删除一些更复杂的查询,比如get_vehicles,假设我可以稍后加载,但这似乎对性能没有显著帮助Ruby on rails Rails作为json的性能问题,ruby-on-rails,json,rails-activerecord,Ruby On Rails,Json,Rails Activerecord,我有一组活动模型记录。阵列中最多有75个对象。然而,该模型具有多个关联和关系。现在我正在重写as_json方法,以便只包含必要的字段。加载60个对象大约需要13秒。我怎样才能加快速度?我尝试过删除一些更复杂的查询,比如get_vehicles,假设我可以稍后加载,但这似乎对性能没有显著帮助 def as_json hash = {} hash["title"] = calendar_title #hash["description"] = calendar_description ha
def as_json
hash = {}
hash["title"] = calendar_title
#hash["description"] = calendar_description
hash["start"] = start_at.strftime("%Y-%m-%d")
hash["end"] = end_at.strftime("%Y-%m-%d")
hash["url"] = "/admin/appointments/#{id}"
hash["base_amount"] = base_amount
hash["start_at"] = start_at
hash["end_at"] = end_at
hash["tech_id"] = technician_id
hash["asset_id"] = asset_id
hash["customer_name"] = customer.full_name(false)
# hash["customer_id"] = customer_id
hash["id"] = id
# hash["cust_addresses"] = customer.addresses
hash["address"] = address
# client_host = ClientHost::Location.find_by_id(address.client_host_locations_id)
# hash["customer_email"] = customer.email
# hash["customer_wholesale"] = customer.wholesale?
# hash["customer_tax_exempt"] = customer.tax_exempt?
# hash["vehicles"] = self.get_vehicles
end
def get_vehicles
vehicles = []
self.appointment_vehicles.each do |av|
hash = {}
hash["appointment_vehicle"] = av
hash["total_amount"] = av.total_amount
hash["vehicle"] = av.vehicle
hash["package_display_title"] = av.package.display_title
hash["package_amount"] = av.package.amount
hash["upgrades"] = self.get_upgrades(av.upgrades)
vehicles.push(hash)
end
return vehicles
结束我不确定能不能把你带到这里,但我会好好挖掘一下
require 'oj'
def get_vehicles
vehicles = self.appointment_vehicles.map do |av|
{
appointment_vehicle: av,
total_amount: av.total_amount,
vehicle: av.vehicle,
package_display_title: av.package.display_title,
package_amount: av.package.amount,
upgrades: self.get_upgrades(av.upgrades)
}
end
vehicles
# Oj.dump(vehicles) <-- if you need this hash as json
end
需要“oj”
def get_车辆
车辆=自行预约_车辆.map do | av|
{
预约车辆:av,
总金额:平均总金额,
车辆:av.vehicle,
包装显示标题:av.package.display标题,
套餐金额:平均套餐金额,
升级:自我升级(av.升级)
}
结束
车辆
#转储(车辆)您可以显示查询以从数据库检索记录吗?