My rails application took so long time to render the view. This view has consisted of child objects.
First of all, My 'course' object belong to 'school'
class Course < ApplicationRecord
belongs_to :school
end
class School < ApplicationRecord
has_many :courses, dependent: :destroy
end
and Controller.
class CoursesController < ApplicationController
before_action :set_course , only: [:show, :edit, :update, :destroy]
# GET /courses
# GET /courses.json
def index
@courses = Course.all
end
// other stuffs..
end
In a View, I have a part for rendering the course's relation with a school in DB.
index.html.erb
<div>
<table>
<thead>
//.....stuffss..
</thead>
<tbody>
<% @courses.each do |course| %>
<tr>
<th><%= course.school.location %></th> // This parts are referencing 'school'..
<td><%= course.school.name %></td>
<td><%= course.name %></td>
<td><%= convert_currency(course.price) %></td>
<td><%= link_to "course", course_path(course.id), :class => 'button' %></td>
<td><%= course.school.nationalmix %></td>
<td><%= course.school.certificate %></td>
<td><%= course.school.good %></td>
<td><%= course.school.bad %></td>
<td><%= link_to "course", course_path(course.id), :class => 'button' %> </td>
</tr>
<% end %>
</tbody>
</table>
</div>
It turned out that rendering page is taking so long time.
Started GET "/courses" for ::1 at 2019-11-21 10:57:23 +0000
Processing by CoursesController#index as HTML
Rendering courses/index.html.erb within layouts/application
Course Load (0.3ms) SELECT "courses".* FROM "courses"
↳ app/views/courses/index.html.erb:102
School Load (0.2ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
↳ app/views/courses/index.html.erb:104
School Load (0.2ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = $1 LIMIT $2 [["id", 5], ["LIMIT", 1]]
↳ app/views/courses/index.html.erb:104
School Load (0.4ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/views/courses/index.html.erb:104
CACHE School Load (0.0ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/views/courses/index.html.erb:104
School Load (0.3ms) SELECT "schools".* FROM "schools" WHERE "schools"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/views/courses/index.html.erb:104
Rendered courses/index.html.erb within layouts/application (Duration: 7941.3ms | Allocations: 184810) // See this line.
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 6], ["LIMIT", 1]]
↳ app/views/layouts/application.html.erb:71
Completed 200 OK in 8015ms (Views: 8011.2ms | ActiveRecord: 1.8ms | Allocations: 205787)
Rendered courses/index.html.erb within layouts/application (Duration: 7941.3ms | Allocations: 184810)
Almost 8 seconds. Is there any better way of doing this?