Rails Always Eager Load association with default_scope doesn't work - ruby-on-rails

I'm trying to Eager Load association Curriculum with default_scope but it doesn't seems to work, since N+1 queries are being generated. These queries exists because of the after_initialize hook.
How can I remove those N+1 queries?
class User < ActiveRecord::Base
has_one :curriculum,
autosave: true,
dependent: :destroy,
inverse_of: :user
default_scope { includes(:curriculum) }
after_initialize :build_curriculum, if: "curriculum.blank?"
end
12:52:53 web.1 | SQL (28.3ms) SELECT DISTINCT "users"."id", "users"."name" AS alias_0, "users"."name" AS alias_1 FROM "users" LEFT OUTER JOIN "curriculums" ON "curriculums"."user_id" = "users"."id" LEFT OUTER JOIN "attendances" ON "attendances"."user_id" = "users"."id" WHERE "attendances"."event_id" = 1 ORDER BY "users"."name" ASC LIMIT 10 OFFSET 0
12:52:53 web.1 | SQL (4.1ms) SELECT "users"."id" AS t0_r0, "users"."name" AS t0_r1, "users"."lastname" AS t0_r2, "users"."created_at" AS t0_r3, "users"."updated_at" AS t0_r4, "users"."email" AS t0_r5, "users"."encrypted_password" AS t0_r6, "users"."reset_password_token" AS t0_r7, "users"."reset_password_sent_at" AS t0_r8, "users"."remember_created_at" AS t0_r9, "users"."sign_in_count" AS t0_r10, "users"."current_sign_in_at" AS t0_r11, "users"."last_sign_in_at" AS t0_r12, "users"."current_sign_in_ip" AS t0_r13, "users"."last_sign_in_ip" AS t0_r14, "users"."auth_token" AS t0_r15, "users"."role" AS t0_r16, "users"."cpf" AS t0_r17, "users"."source" AS t0_r18, "users"."linkedin_campaign_shown_at" AS t0_r19, "curriculums"."id" AS t1_r0, "curriculums"."user_id" AS t1_r1, "curriculums"."university_id" AS t1_r2, "curriculums"."picture" AS t1_r3, "curriculums"."phone" AS t1_r4, "curriculums"."end_year" AS t1_r5, "curriculums"."created_at" AS t1_r6, "curriculums"."updated_at" AS t1_r7, "curriculums"."document" AS t1_r8, "curriculums"."tag_code_conflict" AS t1_r9, "attendances"."id" AS t2_r0, "attendances"."event_id" AS t2_r1, "attendances"."user_id" AS t2_r2, "attendances"."tag_code" AS t2_r3, "attendances"."created_at" AS t2_r4, "attendances"."updated_at" AS t2_r5, "attendances"."tag_code_conflict" AS t2_r6, "attendances"."follows_campaign_shown_at" AS t2_r7 FROM "users" LEFT OUTER JOIN "curriculums" ON "curriculums"."user_id" = "users"."id" LEFT OUTER JOIN "attendances" ON "attendances"."user_id" = "users"."id" WHERE "attendances"."event_id" = 1 AND "users"."id" IN (3095, 3491, 2671, 2693, 1346, 3897, 4312, 5004, 3015, 5541) ORDER BY "users"."name" ASC
12:52:53 web.1 | Curriculum Load (0.3ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 3095]]
12:52:53 web.1 | Curriculum Load (5.4ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 3491]]
12:52:53 web.1 | Curriculum Load (0.3ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 2671]]
12:52:53 web.1 | Curriculum Load (0.3ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 2693]]
12:52:53 web.1 | Curriculum Load (0.3ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 1346]]
12:52:53 web.1 | Curriculum Load (0.3ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 3897]]
12:52:53 web.1 | Curriculum Load (0.3ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 4312]]
12:52:53 web.1 | Curriculum Load (0.3ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 5004]]
12:52:53 web.1 | Curriculum Load (1.5ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 3015]]
12:52:53 web.1 | Curriculum Load (0.4ms) SELECT "curriculums".* FROM "curriculums" WHERE "curriculums"."user_id" = $1 ORDER BY "curriculums"."id" ASC LIMIT 1 [["user_id", 5541]]

Related

Heroku: Why am I losing Memory over the day - Error Logs?

Any help finding what the problem is would be more than appreciated.
My Heroku Metrics are showing "Memory quota exceeded: There have been 2848 memory errors in the current time period." My memory usage dips down to under 256MB then builds up to over 512MB. I'm not sure how long this has been going on for.
I have attached the logs
2019-11-06T23:05:49.278423+00:00 heroku[web.1]: Error R14 (Memory quota exceeded)
2019-11-06T23:06:11.508419+00:00 heroku[web.1]: Process running mem=561M(109.1%)
2019-11-06T23:06:11.508562+00:00 heroku[web.1]: Error R14 (Memory quota exceeded)
2019-11-06T23:06:11.537488+00:00 heroku[router]: at=info method=GET path="/list/search?search=Olive+Oil" host=www.nzchemicalsuppliers.co.nz request_id=c7f01600-87e6-4b8c-8274-7baf460876de fwd="157.55.39.123" dyno=web.1 connect=1ms service=7ms status=301 bytes=260 protocol=http
2019-11-06T23:06:11.532280+00:00 app[web.1]: 10.45.9.232 - - [06/Nov/2019:23:06:11 UTC] "GET /list/search?search=Olive+Oil HTTP/1.1" 301 0
2019-11-06T23:06:11.534556+00:00 app[web.1]: - -> /list/search?search=Olive+Oil
2019-11-06T23:06:15.572013+00:00 heroku[router]: at=info method=GET path="/list/search?search=sodium+lauryl+sulphate" host=www.nzchemicalsuppliers.co.nz request_id=b57d22bc-8604-484b-b1f6-dc2ccf80589c fwd="157.55.39.123" dyno=web.1 connect=1ms service=3ms status=301 bytes=273 protocol=http
2019-11-06T23:06:15.570589+00:00 app[web.1]: 10.69.178.208 - - [06/Nov/2019:23:06:15 UTC] "GET /list/search?search=sodium+lauryl+sulphate HTTP/1.1" 301 0
2019-11-06T23:06:15.570613+00:00 app[web.1]: - -> /list/search?search=sodium+lauryl+sulphate
2019-11-06T23:06:18.831927+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] Started GET "/list/search?search=20_litre+packaging+20" for 66.249.75.149 at 2019-11-06 23:06:18 +0000
2019-11-06T23:06:18.833082+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] Processing by ListController#search as HTML
2019-11-06T23:06:18.833167+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] Parameters: {"search"=>"20_litre packaging 20"}
2019-11-06T23:06:18.838525+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlRedirect Exists (2.1ms)[0m [1m[34mSELECT 1 AS one FROM "url_redirects" WHERE "url_redirects"."from" = $1 LIMIT $2[0m [["from", "/list/search?search=20_litre+packaging+20"], ["LIMIT", 1]]
2019-11-06T23:06:18.842179+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mContent Load (2.0ms)[0m [1m[34mSELECT "contents".* FROM "contents" WHERE "contents"."id" = $1 LIMIT $2[0m [["id", 2], ["LIMIT", 1]]
2019-11-06T23:06:18.846298+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mContent Load (1.8ms)[0m [1m[34mSELECT "contents".* FROM "contents" WHERE "contents"."url_slug" = $1 ORDER BY "contents"."id" ASC LIMIT $2[0m [["url_slug", "search_page"], ["LIMIT", 1]]
2019-11-06T23:06:18.848059+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Content Load (0.0ms)[0m [1m[34mSELECT "contents".* FROM "contents" WHERE "contents"."url_slug" = $1 ORDER BY "contents"."id" ASC LIMIT $2[0m [["url_slug", "search_page"], ["LIMIT", 1]]
2019-11-06T23:06:18.879169+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mSQL (26.7ms)[0m [1m[34mSELECT "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."other_names" AS t0_r2, "products"."company_id" AS t0_r3, "products"."created_at" AS t0_r4, "products"."updated_at" AS t0_r5, "products"."active" AS t0_r6, "products"."company_name" AS t0_r7, "products"."company_url" AS t0_r8, "companies"."id" AS t1_r0, "companies"."name" AS t1_r1, "companies"."active" AS t1_r2, "companies"."contact_name" AS t1_r3, "companies"."contact_phone" AS t1_r4, "companies"."location" AS t1_r5, "companies"."website" AS t1_r6, "companies"."created_at" AS t1_r7, "companies"."updated_at" AS t1_r8, "companies"."logo" AS t1_r9, "companies"."show_logo" AS t1_r10, "companies"."area" AS t1_r11, "companies"."email" AS t1_r12, "companies"."enable_enquiries" AS t1_r13, "companies"."show_supplier_link" AS t1_r14, "companies"."slug" AS t1_r15, "companies"."meta_description" AS t1_r16 FROM "products" LEFT OUTER JOIN "companies" ON "companies"."id" = "products"."company_id" WHERE ((1=1) AND (upper(products.name) LIKE '20_LITRE PACKAGING 20%') AND (upper(products.name) LIKE '20_LITRE PACKAGING 20%') AND (upper(products.name) LIKE '20_LITRE PACKAGING 20%')) ORDER BY products.name ASC[0m
2019-11-06T23:06:18.905935+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mSQL (25.0ms)[0m [1m[34mSELECT "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."other_names" AS t0_r2, "products"."company_id" AS t0_r3, "products"."created_at" AS t0_r4, "products"."updated_at" AS t0_r5, "products"."active" AS t0_r6, "products"."company_name" AS t0_r7, "products"."company_url" AS t0_r8, "companies"."id" AS t1_r0, "companies"."name" AS t1_r1, "companies"."active" AS t1_r2, "companies"."contact_name" AS t1_r3, "companies"."contact_phone" AS t1_r4, "companies"."location" AS t1_r5, "companies"."website" AS t1_r6, "companies"."created_at" AS t1_r7, "companies"."updated_at" AS t1_r8, "companies"."logo" AS t1_r9, "companies"."show_logo" AS t1_r10, "companies"."area" AS t1_r11, "companies"."email" AS t1_r12, "companies"."enable_enquiries" AS t1_r13, "companies"."show_supplier_link" AS t1_r14, "companies"."slug" AS t1_r15, "companies"."meta_description" AS t1_r16 FROM "products" LEFT OUTER JOIN "companies" ON "companies"."id" = "products"."company_id" WHERE ((1=1) AND (upper(products.name) LIKE '%20_LITRE%') AND (upper(products.name) LIKE '%PACKAGING%') AND (upper(products.name) LIKE '%20%')) ORDER BY products.name ASC[0m
2019-11-06T23:06:18.994728+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mSQL (86.0ms)[0m [1m[34mSELECT "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."other_names" AS t0_r2, "products"."company_id" AS t0_r3, "products"."created_at" AS t0_r4, "products"."updated_at" AS t0_r5, "products"."active" AS t0_r6, "products"."company_name" AS t0_r7, "products"."company_url" AS t0_r8, "companies"."id" AS t1_r0, "companies"."name" AS t1_r1, "companies"."active" AS t1_r2, "companies"."contact_name" AS t1_r3, "companies"."contact_phone" AS t1_r4, "companies"."location" AS t1_r5, "companies"."website" AS t1_r6, "companies"."created_at" AS t1_r7, "companies"."updated_at" AS t1_r8, "companies"."logo" AS t1_r9, "companies"."show_logo" AS t1_r10, "companies"."area" AS t1_r11, "companies"."email" AS t1_r12, "companies"."enable_enquiries" AS t1_r13, "companies"."show_supplier_link" AS t1_r14, "companies"."slug" AS t1_r15, "companies"."meta_description" AS t1_r16 FROM "products" LEFT OUTER JOIN "companies" ON "companies"."id" = "products"."company_id" WHERE ((1=1) AND (upper(products.other_names) LIKE '%20_LITRE%') AND (upper(products.other_names) LIKE '%PACKAGING%') AND (upper(products.other_names) LIKE '%20%')) ORDER BY products.name ASC[0m
2019-11-06T23:06:19.002966+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mSQL (2.1ms)[0m [1m[34mSELECT "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."other_names" AS t0_r2, "products"."company_id" AS t0_r3, "products"."created_at" AS t0_r4, "products"."updated_at" AS t0_r5, "products"."active" AS t0_r6, "products"."company_name" AS t0_r7, "products"."company_url" AS t0_r8, "companies"."id" AS t1_r0, "companies"."name" AS t1_r1, "companies"."active" AS t1_r2, "companies"."contact_name" AS t1_r3, "companies"."contact_phone" AS t1_r4, "companies"."location" AS t1_r5, "companies"."website" AS t1_r6, "companies"."created_at" AS t1_r7, "companies"."updated_at" AS t1_r8, "companies"."logo" AS t1_r9, "companies"."show_logo" AS t1_r10, "companies"."area" AS t1_r11, "companies"."email" AS t1_r12, "companies"."enable_enquiries" AS t1_r13, "companies"."show_supplier_link" AS t1_r14, "companies"."slug" AS t1_r15, "companies"."meta_description" AS t1_r16 FROM "products" LEFT OUTER JOIN "companies" ON "companies"."id" = "products"."company_id" WHERE ((1=1) AND (upper(companies.name) LIKE '%20_LITRE%') AND (upper(companies.name) LIKE '%PACKAGING%') AND (upper(companies.name) LIKE '%20%')) ORDER BY products.name ASC[0m
2019-11-06T23:06:19.006503+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mSQL (1.7ms)[0m [1m[34mSELECT "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."other_names" AS t0_r2, "products"."company_id" AS t0_r3, "products"."created_at" AS t0_r4, "products"."updated_at" AS t0_r5, "products"."active" AS t0_r6, "products"."company_name" AS t0_r7, "products"."company_url" AS t0_r8, "companies"."id" AS t1_r0, "companies"."name" AS t1_r1, "companies"."active" AS t1_r2, "companies"."contact_name" AS t1_r3, "companies"."contact_phone" AS t1_r4, "companies"."location" AS t1_r5, "companies"."website" AS t1_r6, "companies"."created_at" AS t1_r7, "companies"."updated_at" AS t1_r8, "companies"."logo" AS t1_r9, "companies"."show_logo" AS t1_r10, "companies"."area" AS t1_r11, "companies"."email" AS t1_r12, "companies"."enable_enquiries" AS t1_r13, "companies"."show_supplier_link" AS t1_r14, "companies"."slug" AS t1_r15, "companies"."meta_description" AS t1_r16 FROM "products" LEFT OUTER JOIN "companies" ON "companies"."id" = "products"."company_id" WHERE ((1=1) AND (upper(companies.area) LIKE '%20_LITRE%') AND (upper(companies.area) LIKE '%PACKAGING%') AND (upper(companies.area) LIKE '%20%')) ORDER BY products.name ASC[0m
2019-11-06T23:06:19.029475+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mSQL (19.3ms)[0m [1m[34mSELECT "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."other_names" AS t0_r2, "products"."company_id" AS t0_r3, "products"."created_at" AS t0_r4, "products"."updated_at" AS t0_r5, "products"."active" AS t0_r6, "products"."company_name" AS t0_r7, "products"."company_url" AS t0_r8, "companies"."id" AS t1_r0, "companies"."name" AS t1_r1, "companies"."active" AS t1_r2, "companies"."contact_name" AS t1_r3, "companies"."contact_phone" AS t1_r4, "companies"."location" AS t1_r5, "companies"."website" AS t1_r6, "companies"."created_at" AS t1_r7, "companies"."updated_at" AS t1_r8, "companies"."logo" AS t1_r9, "companies"."show_logo" AS t1_r10, "companies"."area" AS t1_r11, "companies"."email" AS t1_r12, "companies"."enable_enquiries" AS t1_r13, "companies"."show_supplier_link" AS t1_r14, "companies"."slug" AS t1_r15, "companies"."meta_description" AS t1_r16 FROM "products" LEFT OUTER JOIN "companies" ON "companies"."id" = "products"."company_id" WHERE ((1=1) AND (upper(products.company_name) LIKE '%20_LITRE%') AND (upper(products.company_name) LIKE '%PACKAGING%') AND (upper(products.company_name) LIKE '%20%')) ORDER BY products.name ASC[0m
2019-11-06T23:06:19.159297+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mSQL (127.2ms)[0m [1m[34mSELECT "products"."id" AS t0_r0, "products"."name" AS t0_r1, "products"."other_names" AS t0_r2, "products"."company_id" AS t0_r3, "products"."created_at" AS t0_r4, "products"."updated_at" AS t0_r5, "products"."active" AS t0_r6, "products"."company_name" AS t0_r7, "products"."company_url" AS t0_r8, "companies"."id" AS t1_r0, "companies"."name" AS t1_r1, "companies"."active" AS t1_r2, "companies"."contact_name" AS t1_r3, "companies"."contact_phone" AS t1_r4, "companies"."location" AS t1_r5, "companies"."website" AS t1_r6, "companies"."created_at" AS t1_r7, "companies"."updated_at" AS t1_r8, "companies"."logo" AS t1_r9, "companies"."show_logo" AS t1_r10, "companies"."area" AS t1_r11, "companies"."email" AS t1_r12, "companies"."enable_enquiries" AS t1_r13, "companies"."show_supplier_link" AS t1_r14, "companies"."slug" AS t1_r15, "companies"."meta_description" AS t1_r16 FROM "products" LEFT OUTER JOIN "companies" ON "companies"."id" = "products"."company_id" WHERE ((1=1) AND (upper(products.name) LIKE '%20_LITRE%' OR upper(products.other_names) LIKE '%20_LITRE%' or upper(companies.name) LIKE '%20_LITRE%' or upper(companies.area) LIKE '%20_LITRE%') AND (upper(products.name) LIKE '%PACKAGING%' OR upper(products.other_names) LIKE '%PACKAGING%' or upper(companies.name) LIKE '%PACKAGING%' or upper(companies.area) LIKE '%PACKAGING%') AND (upper(products.name) LIKE '%20%' OR upper(products.other_names) LIKE '%20%' or upper(companies.name) LIKE '%20%' or upper(companies.area) LIKE '%20%')) ORDER BY products.name ASC[0m
2019-11-06T23:06:19.165976+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] Rendering list/search.html.erb within layouts/application
2019-11-06T23:06:19.169205+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.6ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "a"], ["LIMIT", 1]]
2019-11-06T23:06:19.171653+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.2ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "b"], ["LIMIT", 1]]
2019-11-06T23:06:19.174728+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.3ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "c"], ["LIMIT", 1]]
2019-11-06T23:06:19.176851+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.2ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "d"], ["LIMIT", 1]]
2019-11-06T23:06:19.179044+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.3ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "e"], ["LIMIT", 1]]
2019-11-06T23:06:19.181084+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.2ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "f"], ["LIMIT", 1]]
2019-11-06T23:06:19.183162+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.3ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "g"], ["LIMIT", 1]]
2019-11-06T23:06:19.185220+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.2ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "h"], ["LIMIT", 1]]
2019-11-06T23:06:19.187257+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.1ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "i"], ["LIMIT", 1]]
2019-11-06T23:06:19.189302+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.2ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "j"], ["LIMIT", 1]]
2019-11-06T23:06:19.191419+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.3ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "k"], ["LIMIT", 1]]
2019-11-06T23:06:19.193432+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.2ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "l"], ["LIMIT", 1]]
2019-11-06T23:06:19.196123+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.8ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "m"], ["LIMIT", 1]]
2019-11-06T23:06:19.198330+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.3ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "n"], ["LIMIT", 1]]
2019-11-06T23:06:19.201841+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (2.6ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "o"], ["LIMIT", 1]]
2019-11-06T23:06:19.204019+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.3ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "p"], ["LIMIT", 1]]
2019-11-06T23:06:19.206027+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.2ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "q"], ["LIMIT", 1]]
2019-11-06T23:06:19.208547+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.7ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "r"], ["LIMIT", 1]]
2019-11-06T23:06:19.213817+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (4.4ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "s"], ["LIMIT", 1]]
2019-11-06T23:06:19.218069+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (2.3ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "t"], ["LIMIT", 1]]
2019-11-06T23:06:19.223322+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.4ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "u"], ["LIMIT", 1]]
2019-11-06T23:06:19.226364+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.5ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "v"], ["LIMIT", 1]]
2019-11-06T23:06:19.229552+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.4ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "w"], ["LIMIT", 1]]
2019-11-06T23:06:19.232934+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.5ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "x"], ["LIMIT", 1]]
2019-11-06T23:06:19.235729+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (1.3ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "y"], ["LIMIT", 1]]
2019-11-06T23:06:19.242161+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mUrlItem Load (5.1ms)[0m [1m[34mSELECT "url_items".* FROM "url_items" WHERE "url_items"."slug" = $1 ORDER BY "url_items"."id" ASC LIMIT $2[0m [["slug", "z"], ["LIMIT", 1]]
2019-11-06T23:06:19.243450+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] Rendered includes/_search.html.erb (77.0ms)
2019-11-06T23:06:19.281127+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mSetting Load (36.9ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.284426+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] Rendered includes/_enquiry_modal.html.erb (0.1ms)
2019-11-06T23:06:19.285496+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.286109+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.286757+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.287338+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.299435+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35m (10.2ms)[0m [1m[34mSELECT COUNT(*) FROM "products" WHERE "products"."company_id" = $1[0m [["company_id", 209]]
2019-11-06T23:06:19.300193+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.301254+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.306744+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.308413+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.308467+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.314453+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.316544+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mCACHE (0.0ms)[0m [1m[34mSELECT COUNT(*) FROM "products" WHERE "products"."company_id" = $1[0m [["company_id", 209]]
2019-11-06T23:06:19.317021+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.320292+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.322974+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.1ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.323724+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.324372+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.325057+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.337687+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35m (10.4ms)[0m [1m[34mSELECT COUNT(*) FROM "products" WHERE "products"."company_id" = $1[0m [["company_id", 292]]
2019-11-06T23:06:19.338275+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.339041+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.340498+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.341021+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.341508+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.341964+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.342960+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mCACHE (0.0ms)[0m [1m[34mSELECT COUNT(*) FROM "products" WHERE "products"."company_id" = $1[0m [["company_id", 209]]
2019-11-06T23:06:19.343327+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.343940+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.345260+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.345842+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.346300+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.346731+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.358789+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35m (10.8ms)[0m [1m[34mSELECT COUNT(*) FROM "products" WHERE "products"."company_id" = $1[0m [["company_id", 107]]
2019-11-06T23:06:19.359446+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.360168+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.361659+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.362178+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.362638+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.363074+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[36mCACHE Setting Load (0.0ms)[0m [1m[34mSELECT "settings".* FROM "settings" WHERE "settings"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]]
2019-11-06T23:06:19.364066+00:00 app[web.1]: [d5fbbd9d-3e4a-488c-8e02-beca6b8e8916] [1m[35mCACHE (0.0ms)[0m [1m[34mSELECT COUNT(*) FROM "products" WHERE "products"."company_id" = $1[0m [["company_id", 292]]```
I have an edit for my answer after further research.
We have the same problem with our heroku apps. I looked at the New Relic analysis and discovered that while heroku reports the app is using up to 5 Gigabytes, the ruby heap only grows to 3 Megabytes. Further, while heroku reports the app's memory grows in a stead linear growth until the dyno is restarted, New Relic shows the ruby heap grows and shrinks.
I have concluded that the problem is with the ruby allocator. This is something you can do little about. You can however, switch to a ruby compiled using the jemalloc library.
This article Malloc Can Double Multi-threaded Ruby Program Memory Usage explains.
An heroku build pack for jemalloc can be this one found on github, gaffneyc/heroku-buildpack-jemalloc.
FROM BEFORE EDIT:
I did find an article What causes Ruby memory bloat? which recommends setting MALLOC_ARENA_MAX=2, and upgraded to ruby 2.6.
heroku config:add MALLOC_ARENA_MAX=2 --remote production
I didn't see a big difference for my app.
The article ruby-memory-environment-variables-simpler-than-they-look helped with an easy way to tune some garbage collection settings, but they have to do with how much memory is allocated at a time, which is not much help.
There are plans for ruby 2.7 to have a GC.compact command, which I look forward to helping.

How to avoid N+1 queries for has_many :through associations

I'm building a GraphQL API with Ruby on Rails and the graphql gem. Now I have some n:m relations, like Projects have many Users and Users have many Projects. My models are like:
# /app/models/project.rb
has_many :project_assignments
has_many :project_managers, through: :project_assignments, source: :user
# /app/models/project_assignment.rb
belongs_to :project
belongs_to :user
# /app/models/user.rb
has_many :project_assignments
has_many :projects, through: :project_assignments
Now I want to query all projects and their corresponding project managers with a query like this:
query {
projects {
edges {
node {
id
projectManagers {
edges {
node {
id
}
}
}
}
}
}
}
And my resolvers are basically like Project.all and each Project calls project.projectManagers, which results in hundreds of queries:
Project Load (3.3ms) SELECT "projects".* FROM "projects"
User Load (1.5ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 2]]
User Load (0.7ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 3]]
User Load (1.1ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 4]]
User Load (1.0ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 5]]
User Load (0.8ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 6]]
...
I've added the bullet gem, but there is no warning about a missing eager loading. In fact, If I use Project.all.includes(:project_managers), I get the query that I want (SELECT "project_assignments".* FROM "project_assignments" WHERE "project_assignments"."project_id" IN ($1, $2, $3, $4, $5, $6, $7, $8, ...)), but the User queries fires anyhow:
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN ($1, $2, $3) [["id", 3], ["id", 2], ["id", 1]]
User Load (0.8ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 2]]
User Load (0.9ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 3]]
User Load (0.8ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 4]]
User Load (0.7ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 5]]
User Load (0.7ms) SELECT "users".* FROM "users" INNER JOIN "project_assignments" ON "users"."id" = "project_assignments"."user_id" WHERE "project_assignments"."project_id" = $1 [["project_id", 6]]
...
Is there anything that I can do the preload the users?
I tried eager_load also, but the result is basically the same (tried another example with an normal has_many association (no :through):
SQL (1.3ms) SELECT "projects"."id" AS t0_r0, "projects"."title" AS t0_r1, "projects"."number" AS t0_r2, "projects"."description" AS t0_r3, "projects"."deadline" AS t0_r4, "projects"."archived" AS t0_r5, "projects"."customer_id" AS t0_r6, "projects"."rate_type" AS t0_r7, "projects"."daily_rate" AS t0_r8, "projects"."service_rates" AS t0_r9, "projects"."budget_type" AS t0_r10, "projects"."budget_rate" AS t0_r11, "projects"."created_at" AS t0_r12, "projects"."updated_at" AS t0_r13, "projects"."status" AS t0_r14, "projects"."slug" AS t0_r15, "project_labels"."id" AS t1_r0, "project_labels"."title" AS t1_r1, "project_labels"."description" AS t1_r2, "project_labels"."color" AS t1_r3, "project_labels"."project_id" AS t1_r4, "project_labels"."created_at" AS t1_r5, "project_labels"."updated_at" AS t1_r6 FROM "projects" LEFT OUTER JOIN "project_labels" ON "project_labels"."project_id" = "projects"."id" WHERE "projects"."id" IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) [["id", 1], ["id", 2], ["id", 3], ["id", 4], ["id", 5], ["id", 6], ["id", 7], ["id", 8], ["id", 9], ["id", 10]]
ProjectLabel Load (0.3ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 10]]
ProjectLabel Load (0.5ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 2]]
ProjectLabel Load (0.7ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 5]]
ProjectLabel Load (0.5ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 8]]
ProjectLabel Load (0.5ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 6]]
ProjectLabel Load (0.6ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 4]]
ProjectLabel Load (0.4ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 1]]
ProjectLabel Load (0.3ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 3]]
ProjectLabel Load (0.3ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 9]]
ProjectLabel Load (0.5ms) SELECT "project_labels".* FROM "project_labels" WHERE "project_labels"."project_id" = $1 [["project_id", 7]]
You can do all sorts of things with has_many including includes which should help with N+1 queries
has_many :project_assignments, -> { includes(:projects) }
Project.all.includes(:project_managers)
# Project.includes(:project_managers) # shorthand of above
... would include project_managers automatically in the SQL depending on the query.
To also include project_assignment.user, then just merge them:
Project.includes(:project_managers, project_assignments: :user)
# probably below is equivalent of above (but not sure)
# just because `project_managers` association is also going "through" :project_assignments
Project.includes(project_assignments: :user)
See "Loading nested relationships" here in the docs

Active Model Serializer - Increasing Render Performance

I'm running into a particular situation where the rendered json generated by ActiveModel::Serializer is extraordinary slow (around 6-8 seconds). How can I improve the speed of this rendering? Here's the code.
Models:
class Comment < ActiveRecord::Base
has_many :children_comments,
class_name: 'Comment',
foreign_key: 'parent_comment_id'
belongs_to :user
belongs_to :parent_comment,
class_name: 'Comment',
foreign_key: 'parent_comment_id'
end
Serializers:
class CommentSerializer < ActiveModel::Serializer
include ActionView::Helpers::DateHelper
attributes :id, :message, :created_at_in_words,
:created_at, :parent_comment_id
belongs_to :user
has_many :children_comments
def created_at_in_words
time_ago_in_words(object.created_at) + ' ago'
end
def children_comments
object.children_comments.map do |comment|
CommentSerializer.new(comment).as_json
end
end
end
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :avatar_url
def avatar_url
object.avatar.url
end
end
In my controller I have
parent_comments = Comment.where(parent_comment_id: nil)
render status: :ok,
json: parent_comments,
each_serializer: CommentSerializer,
key_transform: :camel_lower
Here is my partial log output when I make the call to the server. As you ca see Active Model Serializer is taking around 20ms to make each query call.
Started GET "/comments?lesson_id=420" for ::1 at 2016-09-01 11:09:14 -0400
Processing by Api::CommentsController#index as HTML
Parameters: {"lesson_id"=>"420"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 102]]
Lesson Load (0.5ms) SELECT "lessons".* FROM "lessons" WHERE "lessons"."id" = $1 ORDER BY position ASC LIMIT 1 [["id", 420]]
Comment Load (53.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 AND "comments"."parent_comment_id" IS NULL [["commentable_id", 420], ["commentable_type", "Lesson"]]
[active_model_serializers] User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (24.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41401]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41402]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (22.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41403]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (21.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41404]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41405]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41406]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41407]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41408]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41409]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41410]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41411]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41412]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41413]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (23.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41414]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41415]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41416]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (23.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41417]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41418]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41419]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41420]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41421]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.9ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41422]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41423]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41424]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41425]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41426]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41427]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41428]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41429]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41430]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.9ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41431]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41432]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41433]]
[active_model_serializers] CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (21.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41434]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41435]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (21.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41436]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41437]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41438]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (22.9ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41439]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41440]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41441]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.9ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41442]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41443]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41444]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41445]]
[active_model_serializers] CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41446]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41447]]
[active_model_serializers] CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41448]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41449]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41450]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41451]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41452]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41453]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41454]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (22.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41455]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (22.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41456]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41457]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41458]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41459]]
[active_model_serializers] CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41460]]
[active_model_serializers] CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41461]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41462]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41463]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41464]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41465]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41466]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41467]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41468]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41469]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41470]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41471]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41472]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41473]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41474]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41475]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41476]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (19.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41477]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41478]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41479]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41480]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41534]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41535]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.6ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41536]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.8ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41537]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (18.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 41538]]
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Json (3895.33ms)
Completed 200 OK in 4007ms (Views: 1222.1ms | ActiveRecord: 2743.8ms)
With Michal's answer, here is a small sample from the log.
Started GET "/comments?lesson_id=370" for ::1 at 2016-09-02 17:13:06 -0400
Processing by Api::CommentsController#index as HTML
Parameters: {"lesson_id"=>"370"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 102]]
Lesson Load (0.4ms) SELECT "lessons".* FROM "lessons" WHERE "lessons"."id" = $1 ORDER BY position ASC LIMIT 1 [["id", 370]]
Comment Load (23.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" IS NULL AND "comments"."commentable_type" = 'Lesson' AND "comments"."commentable_id" = 370
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (102)
Comment Load (25.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" IN (38641, 38687, 38733)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (102)
[active_model_serializers] Comment Load (20.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 38642]]
[active_model_serializers] User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (20.7ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 38643]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
[active_model_serializers] Comment Load (30.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."parent_comment_id" = $1 [["parent_comment_id", 38644]]
[active_model_serializers] CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 102]]
My theory is this. I'm convinced the children_comments serialization is causing the majority of the performance problems. Because I have to call children_comments for every comment, this results in a cascading effect. I wonder if I can rewrite the code in a way to improve performance.
You're running into a n+1 queries type problem. Unfortunately includes isn't really helping you here because it only helps you with one level of the association - it avoids separate fetches of the children of the top level comment, but not grand children or great grandchildren.
You could probably optimise the user lookup by maintaining your own cache of user id to user objects (the rails cache caches the raw data but will be reinstantiating the objects over and over again), but to make this substantially faster you need to change how you load the comments.
If you are using a database that supports it (such as postgresql), then recursive queries ar an option (see https://hashrocket.com/blog/posts/recursive-sql-in-activerecord for a worked example). I don't know this scales as the tree gets deeper and deeper.
If recursive queries aren't an option, then there are a few approaches that involve changing what you store.
One is the materialised path patten. For example say that the root comment has id 1, a child has id 101 and one of its children has id 426. That last comment's path is 1/101/426. All of its siblings have paths starting with 1/101/. This means you can use like queries (with wildcards at the end) to find subtrees quickly. The ancestry gem implements this. If comments get moved then you need to rewrite the paths of all the comments children (and grand children etc.), but that may not be relevant to your use case. Very deep trees are problematic I think.
Another is the nested set pattern. The core idea is that the parent node stores the minimum and maximum id of all of its children and their children's children etc. This allows retrieving of all these children in one go. The flip side is that inserts and updates require rewriting a lot of this data (more so than with materialised path). There have been various gems that implement this over the years (a current one seems to be awesome_nested_set).
Also worth checking that you have got the right indexes to support your queries - unless there are really quite a lot of comments with a given parent, 20-30ms seems quite a long time for one query.
Change your ActiveRecord query to this
parent_comments = Comment.where(parent_comment_id: nil).includes(:user, children_comments: :user)
It will get rid of N + 1 queries.

Double everything in my db logs Rails

I have double everything in my logs. Not sure how to get rid of it. Any suggestions?
Bid Load (0.2ms) SELECT "bids".* FROM "bids" WHERE "bids"."order_id" = $1 [["order_id", 7]]
Bid Load (0.2ms) SELECT "bids".* FROM "bids" WHERE "bids"."order_id" = $1 [["order_id", 7]]
Size Load (0.2ms) SELECT "sizes".* FROM "sizes" WHERE "sizes"."order_id" = $1 LIMIT 1 [["order_id", 7]]
Size Load (0.2ms) SELECT "sizes".* FROM "sizes" WHERE "sizes"."order_id" = $1 LIMIT 1 [["order_id", 7]]
Bid Load (0.2ms) SELECT "bids".* FROM "bids" WHERE "bids"."order_id" = $1 [["order_id", 8]]
Bid Load (0.2ms) SELECT "bids".* FROM "bids" WHERE "bids"."order_id" = $1 [["order_id", 8]]
Size Load (0.3ms) SELECT "sizes".* FROM "sizes" WHERE "sizes"."order_id" = $1 LIMIT 1 [["order_id", 8]]
Size Load (0.3ms) SELECT "sizes".* FROM "sizes" WHERE "sizes"."order_id" = $1 LIMIT 1 [["order_id", 8]]
Bid Load (0.2ms) SELECT "bids".* FROM "bids" WHERE "bids"."order_id" = $1 [["order_id", 9]]
Bid Load (0.2ms) SELECT "bids".* FROM "bids" WHERE "bids"."order_id" = $1 [["order_id", 9]]
Size Load (0.2ms) SELECT "sizes".* FROM "sizes" WHERE "sizes"."order_id" = $1 LIMIT 1 [["order_id", 9]]
Size Load (0.2ms) SELECT "sizes".* FROM "sizes" WHERE "sizes"."order_id" = $1 LIMIT 1 [["order_id", 9]]
Bid Load (0.2ms) SELECT "bids".* FROM "bids" WHERE "bids"."order_id" = $1 [["order_id", 10]]
Bid Load (0.2ms) SELECT "bids".* FROM "bids" WHERE "bids"."order_id" = $1 [["order_id", 10]]
CACHE (0.0ms) SELECT "printers".* FROM "printers" WHERE "printers"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "printers".* FROM "printers" WHERE "printers"."id" = $1 LIMIT 1 [["id", 1]]
Size Load (0.2ms) SELECT "sizes".* FROM "sizes" WHERE "sizes"."order_id" = $1 LIMIT 1 [["order_id", 10]]
Size Load (0.2ms) SELECT "sizes".* FROM "sizes" WHERE "sizes"."order_id" = $1 LIMIT 1 [["order_id", 10]]
Rails Version Rails 4.2.5
Ruby Version 2.3.0
you can add the following to application.rb
if Rails.env.development?
# Don't log to STDOUT, by default rails s will handle it
config.logger = Logger.new('/dev/null')
else
# Don't log to file, sending everything to unicorn file.
config.logger = Logger.new(STDOUT)
end
Basically rails server and rails logger both send their messages to stdout which results in duplication in the logs.

Completed 500 Internal Server Error in 142ms (Views: 9.8ms | ActiveRecord: 34.6ms)

So, I have an application that allows users to create events and activities and others can view them (in the show page) and attend if they'd like.
Now the thing is, I'm able to click on an Event and the event#show page pops up and everything is great, incidentally the Activity page doesn't open, I get a 404 error.
When checking the heroku logs, and the development console logs in development I don't see any visible errors, the only error I see is:
Completed 500 Internal Server Error in 142ms (Views: 9.8ms | ActiveRecord: 34.6ms)
What could this be? I've looked over the code several times and there aren't any obvious problems, and also it works in development I only get this 404 in production.. Any ideas?
Update: Heroku logs:
2014-04-07T17:08:57.651579+00:00 app[web.1]: Parameters: {"id"=>"4"}
2014-04-07T17:08:57.651738+00:00 app[web.1]: Geokit is using the domain: *hidden*
2014-04-07T17:08:57.657087+00:00 app[web.1]: User Load (3.8ms) SELECT "users".* FROM "users" ORDER BY random() LIMIT 5
2014-04-07T17:08:57.663588+00:00 app[web.1]: User Load (4.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 40 ORDER BY "users"."id" ASC LIMIT 1
2014-04-07T17:08:57.672547+00:00 app[web.1]: Activity Load (7.7ms) SELECT "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT 1 [["id", "4"]]
2014-04-07T17:08:57.676877+00:00 app[web.1]: User Load (2.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 38]]
2014-04-07T17:08:57.693696+00:00 app[web.1]: Photo Load (1.3ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 40], ["attachable_type", "User"]]
2014-04-07T17:08:57.697251+00:00 app[web.1]: Ethnicity Load (1.0ms) SELECT "ethnicities".* FROM "ethnicities" WHERE "ethnicities"."id" = $1 ORDER BY "ethnicities"."id" ASC LIMIT 1 [["id", 6]]
2014-04-07T17:08:57.699600+00:00 app[web.1]: Education Load (1.5ms) SELECT "educations".* FROM "educations" WHERE "educations"."id" = $1 ORDER BY "educations"."id" ASC LIMIT 1 [["id", 4]]
2014-04-07T17:08:57.710452+00:00 app[web.1]: Question Load (9.6ms) SELECT "questions".* FROM "questions" WHERE "questions"."user_id" = $1 [["user_id", 40]]
2014-04-07T17:08:57.712549+00:00 app[web.1]: CACHE (0.0ms) SELECT "photos".* FROM
"photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 40], ["attachable_type", "User"]]
2014-04-07T17:08:57.728908+00:00 app[web.1]: CACHE (15.5ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 40], ["attachable_type", "User"]]
2014-04-07T17:08:57.730101+00:00 app[web.1]: CACHE (0.0ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 40], ["attachable_type", "User"]]
2014-04-07T17:08:57.730981+00:00 app[web.1]: CACHE (0.1ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 40], ["attachable_type", "User"]]
2014-04-07T17:08:57.761039+00:00 app[web.1]: Rendered layouts/_left_side.html.erb (72.0ms)
2014-04-07T17:08:57.731304+00:00 app[web.1]: Rendered layouts/_profile_completion.html.erb (19.6ms)
2014-04-07T17:08:57.760362+00:00 app[web.1]: Message Exists (3.4ms) SELECT 1 AS one FROM "messages" WHERE "messages"."recipient_id" = $1 AND "messages"."is_new" = 't' LIMIT 1 [["recipient_id", 40]]
2014-04-07T17:08:57.786347+00:00 app[web.1]: CACHE (0.1ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 40], ["attachable_type", "User"]]
2014-04-07T17:08:57.782607+00:00 app[web.1]: (1.6ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 [["commentable_id", 4], ["commentable_type", "Activity"]]
2014-04-07T17:08:57.799144+00:00 app[web.1]: CACHE (0.0ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 40], ["attachable_type", "User"]]
2014-04-07T17:08:57.818837+00:00 app[web.1]: Rendered collection (0.0ms)
2014-04-07T17:08:57.833708+00:00 app[web.1]: User Exists (5.2ms) SELECT 1 AS one FROM "users" INNER JOIN "activity_participants" ON "users"."id" = "activity_participants"."user_id" WHERE "activity_participants"."activity_id" = $1 AND "users"."id" = 40 LIMIT 1 [["activity_id", 4]]
2014-04-07T17:08:57.840280+00:00 app[web.1]: Photo Load (3.9ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 38], ["attachable_type", "User"]]
2014-04-07T17:08:57.818561+00:00 app[web.1]: Comment Load (5.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2 AND "comments"."parent_id" IS NULL [["commentable_id", 4], ["commentable_type", "Activity"]]
2014-04-07T17:08:57.841447+00:00 app[web.1]: CACHE (0.0ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 38], ["attachable_type", "User"]]
2014-04-07T17:08:57.858143+00:00 app[web.1]: (5.4ms) SELECT COUNT(*) FROM "users" INNER JOIN "activity_participants" ON "users"."id" = "activity_participants"."user_id" WHERE "activity_participants"."activity_id" = $1 [["activity_id", 4]]
2014-04-07T17:08:57.863920+00:00 app[web.1]: CACHE (0.0ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 38], ["attachable_type", "User"]]
2014-04-07T17:08:57.861996+00:00 app[web.1]: User Load (3.0ms) SELECT "users".* FROM "users" INNER JOIN "activity_participants" ON "users"."id" = "activity_participants"."user_id" WHERE "activity_participants"."activity_id" = $1 [["activity_id", 4]]
2014-04-07T17:08:57.864761+00:00 app[web.1]: CACHE (0.0ms) SELECT "photos".* FROM "photos" WHERE "photos"."attachable_id" = $1 AND "photos"."attachable_type" = $2 AND "photos"."profile_photo" = 't' ORDER BY "photos"."id" ASC LIMIT 1 [["attachable_id", 38], ["attachable_type", "User"]]
2014-04-07T17:08:57.869745+00:00 app[web.1]: Rendered activities/_activity_right_side.html.erb (45.4ms)
2014-04-07T17:08:57.869976+00:00 app[web.1]: Rendered activities/show.html.erb within layouts/application (184.0ms)
2014-04-07T17:08:57.871758+00:00 app[web.1]: Rendered errors/error_500.html.erb within layouts/application (0.5ms)
2014-04-07T17:08:57.875144+00:00 app[web.1]: Event Load (1.5ms) SELECT "events".* FROM "events" INNER JOIN "event_participants" ON "events"."id" = "event_participants"."event_id" WHERE "event_participants"."user_id" = $1 [["user_id", 40]]
2014-04-07T17:08:57.875788+00:00 app[web.1]: Rendered layouts/_pusher.html.erb (3.1ms)
2014-04-07T17:08:57.880542+00:00 app[web.1]: Notification Exists (1.1ms) SELECT 1 AS one FROM "notifications" WHERE "notifications"."user_id" = $1 AND "notifications"."is_opened" = 'f' LIMIT 1 [["user_id", 40]]
2014-04-07T17:08:57.882166+00:00 app[web.1]: CACHE (0.0ms) SELECT 1 AS one FROM "notifications" WHERE "notifications"."user_id" = $1 AND "notifications"."is_opened" = 'f' LIMIT 1 [["user_id", 40]]
2014-04-07T17:08:57.891660+00:00 heroku[router]: at=info method=GET path=/activities/4 host=*hidden* request_id=f6f6ab0e-c627-482d-a49b-d7d351ba398a fwd="99.234.25.253" dyno=web.1 connect=9ms service=393ms status=500 bytes=10914
2014-04-07T17:08:57.884160+00:00 app[web.1]: CACHE (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."recipient_id" = $1 AND "messages"."is_new" = 't' LIMIT 1 [["recipient_id", 40]]
2014-04-07T17:08:57.883375+00:00 app[web.1]: CACHE (0.0ms) SELECT 1 AS one FROM "messages" WHERE "messages"."recipient_id" = $1 AND "messages"."is_new" = 't' LIMIT 1 [["recipient_id", 40]]
2014-04-07T17:08:57.884773+00:00 app[web.1]: Rendered shared/_header.html.erb (8.2ms)
2014-04-07T17:08:57.885126+00:00 app[web.1]: Rendered layouts/_flash.html.erb (0.1ms)
2014-04-07T17:08:57.885902+00:00 app[web.1]: Completed 500 Internal Server Error in 234ms (Views: 12.7ms | ActiveRecord: 81.1ms)
2014-04-07T17:08:57.885600+00:00 app[web.1]: Rendered shared/_footer.html.erb (0.2ms)
2014-04-07T17:08:58.357410+00:00 heroku[router]: at=info method=GET path=/assets/logo-404-6d97dc33614d27e176e8344ea679813f.png host=www.friendiose.com request_id=d00190ed-d365-46aa-978d-53c0fed636f4 fwd="99.234.25.253" dyno=web.1 connect=3ms service=12ms status=200 bytes=30821
2014-04-07T17:09:04.078418+00:00 heroku[run.8642]: State changed from up to complete
2014-04-07T17:09:04.066492+00:00 heroku[run.8642]: Process exited with status 0
2014-04-07T17:08:56.083949+00:00 heroku[router]: at=info method=GET path=/assets/place_map_icon-eea0b812cae352793b07d4cc9ab29ab6.png host=www.friendiose.com request_id=288b92d3-46fc-4d3c-a6c2-edcb1746aa7b fwd="99.234.25.253" dyno=web.1 connect=15ms service=37ms status=200 bytes=3282
2014-04-07T17:08:55.818610+00:00 heroku[router]: at=info method=GET path=/assets/w_icon-b05939cd15957a224c909f4152f6de76.png host=www.friendiose.com request_id=8558679d-8b23-424a-b285-0a5756d637c9 fwd="99.234.25.253" dyno=web.1 connect=4ms service=9ms status=304 bytes=89
2014-04-07T17:10:45.276715+00:00 app[web.1]: I, [2014-04-07T17:10:45.276355 #2] INFO -- : Started GET "/activities/4" for 99.234.25.253 at 2014-04-07 17:10:45 +0000
2014-04-07T17:10:45.276873+00:00 app[web.1]: I, [2014-04-07T17:10:45.276522 #2] INFO -- : Started GET "/activities/4" for 99.234.25.253 at 2014-04-07 17:10:45 +0000

Resources