我有一个非常大的 rails 应用程序,它运行良好,但是有一个页面非常慢!完成加载大约需要 2 分钟。该页面包含一个日历,我在此方法中使用 Json 请求插入了很多约会。
def get_ticket
## get days between event and -10 days and + 10 days
cstart = ((Time.at(params[:start].to_i).to_datetime)-10).to_i # assign to cstart the start date converted into integer
cend = ((Time.at(params[:end].to_i).to_datetime)+10).to_i # assign to cend the end date converted into integer
name = params[:search] # assign to name the param search
option = params[:type] # assign to option the param type
unless current_user.roles[0].name == "admin"
if(name == nil && option == nil)
option = "default_user_search"
name = current_user.district.name
end
end
@data = [] # @data is an empty array
@calendar = Calendar.where("startdate <= #{cstart} AND enddate < #{cend}")
@calendar.each do |i|
if(i.tickets == [])
if(current_user.roles[0].name == "cse")# || current_user.roles[0].name == "ase")
if(i.user == current_user)
@data << {type: "user" , data: UserJson.to_h(i.id,(i.startdate-7200),(i.enddate-7200),i.user,i.status)}
end
else
if(option != nil)
if(i.user.district == District.find_by_name(name))
@data << {type: "user" , data: UserJson.to_h(i.id,(i.startdate-7200),(i.enddate-7200),i.user,i.status)}
end
else
@data << {type: "user" , data: UserJson.to_h(i.id,(i.startdate-7200),(i.enddate-7200),i.user,i.status)}
end
end
else
if(current_user.roles[0].name == "cse")# || current_user.roles[0].name == "ase")
if(i.tickets[0].workers.include? current_user)
p i.tickets[0].percentage
@data << {type: "ticket", data: TicketJson.to_h((i.startdate-7200),(i.enddate-7200),i.tickets,i.tickets[0].percentage.to_i,i.tickets[0].recruiter.surname)}
end
else
if(option != nil)
i.tickets[0].workers.each do |worker|
if(worker.district.name == District.find_by_name(name).name)
p i.tickets[0].percentage
@data << {type: "ticket", data: TicketJson.to_h((i.startdate-7200),(i.enddate-7200),i.tickets,i.tickets[0].percentage.to_i, i.tickets[0].recruiter.surname)}
break
end
end
else
p i.tickets[0].percentage
@data << {type: "ticket", data: TicketJson.to_h((i.startdate-7200),(i.enddate-7200),i.tickets,i.tickets[0].percentage.to_i, i.tickets[0].recruiter.surname)}
end
end
end
end
respond_to do |ff| # The request can be either:
ff.html { render :json => @data} # html, with a json render of @data
ff.json { render :json => @data} # json, with a json render of @data
end
end
我真的不知道如何加快这个动作,如果你需要一些其他的代码,请问我。
希望有人能帮助我,谢谢。