I'm using activeadmin for showing branch model records, the branch model has many branchphones, and I want to show the first branchphone created for this branch as an extra custom attribute on activeadmin branch model screen, so I wrote it like that
show do
attributes_table do
row :id
row :manager_name
row :manager_email
row :phone_number do |branch|
branch&.branch_phones&.order(created_at: :asc)&.first&.phone_number
end
end
end
index do
column :id
column :manager_name
column :manager_email
column :phone_number do |branch|
branch&.branch_phones&.order(created_at: :asc)&.first&.phone_number
end
actions
end
the problem with that code is that it causes me n+1 queries, every time the code get a branch, it goes and create an extra query inside it to get the branch's branch phones, which results on something like that when calling the branch model screen on activeadmin
Processing by Admin::BranchesController#index as HTML
Parameters: {"subdomain"=>""}
AdminUser Load (0.8ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering /usr/local/bundle/gems/activeadmin-2.7.0views/active_admin/resource/index.html.arb
(0.6ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "branches" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
CACHE (0.1ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "branches" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
(0.4ms) SELECT COUNT(*) FROM "branches"
CACHE (0.0ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "branches" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
Branch Load (0.5ms) SELECT "branches".* FROM "branches" ORDER BY "branches"."id" desc LIMIT $1 OFFSET $2 [["LIMIT", 30], ["OFFSET", 0]]
Store Load (0.6ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" IN ($1, $2, $3) [["id", 21], ["id", 1], ["id", 2]]
BranchPhone Load (0.4ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) [["branch_id", 25], ["branch_id", 24], ["branch_id", 22], ["branch_id", 20], ["branch_id", 19], ["branch_id", 14], ["branch_id", 11], ["branch_id", 4], ["branch_id", 3], ["branch_id", 2], ["branch_id", 1]]
BranchPhone Load (0.7ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 25], ["LIMIT", 1]]
BranchPhone Load (0.5ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 24], ["LIMIT", 1]]
BranchPhone Load (0.3ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 22], ["LIMIT", 1]]
BranchPhone Load (0.6ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 20], ["LIMIT", 1]]
BranchPhone Load (0.3ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 19], ["LIMIT", 1]]
BranchPhone Load (0.3ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 14], ["LIMIT", 1]]
BranchPhone Load (0.2ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 11], ["LIMIT", 1]]
BranchPhone Load (0.2ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 4], ["LIMIT", 1]]
BranchPhone Load (0.3ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 3], ["LIMIT", 1]]
BranchPhone Load (0.2ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 2], ["LIMIT", 1]]
BranchPhone Load (0.2ms) SELECT "branch_phones".* FROM "branch_phones" WHERE "branch_phones"."branch_id" = $1 ORDER BY "branch_phones"."created_at" ASC LIMIT $2 [["branch_id", 1], ["LIMIT", 1]]
Rendered /usr/local/bundle/gems/activeadmin-2.7.0views/active_admin/resource/index.html.arb (219.4ms)
Completed 200 OK in 258ms (Views: 208.7ms | ActiveRecord: 23.2ms)
so, any solutions to eliminate the n+1 query for the branch phone ?
knowing that I tried to eliminate it using controller actions like this
but still the branch phones N+1 query not solved
You are getting (N+1) query because you are ordering branch_phones. By default Rails returns records in assenting order. You don't have to order again.
I believe you have some association like bellow:
# app/models/branch.rb
class Branch < ApplicationRecord
has_many :branch_phones
end
# app/models/branch_phone.rb
class BranchPhone < ApplicationRecord
belongs_to :branch
end
Please add this lines in your app/admin/brach.rb file.
# app/admin/brach.rb
controller do
def scoped_collection
super.includes(:branch_phones)
end
end
index do
column :id
column :manager_name
column :manager_email
column :phone_number do |branch|
branch.branch_phones.first&.phone_number
end
actions
end
Now the includes method is available inside the ActiveAdmin. All you need to do is add this into the desired admin file.
# app/admin/brach.rb
ActiveAdmin.register Branch do
includes :branch_phones
index do
column :id
column :manager_name
column :manager_email
column :phone_number do |branch|
branch&.branch_phones&.order(created_at: :asc)&.first&.phone_number
end
actions
end
end
Just streamlined my dev environment and switched from SQLite3 to PG so it's the same on my production environment.
The same working code that I had before is now taking much, MUCH more time than it did before.
Haven't changed anything except installing pg.
Before PG:
Processing by CollectionsController#show as HTML
Parameters: {"keyword"=>"cat and dog towel", "id"=>"37"}
Collection Load (0.2ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = ? LIMIT ? [["id", 37], ["LIMIT", 1]]
Seller Load (0.2ms) SELECT "sellers".* FROM "sellers" WHERE "sellers"."id" = ? LIMIT ? [["id", 13], ["LIMIT", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.1ms) begin transaction
(0.1ms) commit transaction
Search Load (0.2ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = ? LIMIT ? [["term", "cat and dog towel"], ["LIMIT", 1]]
Listing Load (1.4ms) SELECT DISTINCT "listings".* FROM "listings" INNER JOIN "listings_searches" ON "listings"."id" = "listings_searches"."listing_id" WHERE "listings_searches"."search_id" = ? ORDER BY "listings"."id" ASC LIMIT ? [["search_id", 775], ["LIMIT", 1]]
Search Load (0.3ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = ? ORDER BY "searches"."id" ASC LIMIT ? [["term", "Beach Towel"], ["LIMIT", 1]]
(0.1ms) begin transaction
Search Exists (0.2ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = ? LIMIT ? [["term", "Beach Towel"], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES (?, ?, ?) [["term", "Beach Towel"], ["created_at", "2018-06-18 12:47:58.519223"], ["updated_at", "2018-06-18 12:47:58.519223"]]
(7.0ms) commit transaction
Item Exists (0.3ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = ? AND "items"."collection_id" = 37 LIMIT ? [["search_id", 776], ["LIMIT", 1]]
(0.1ms) begin transaction
Collection Load (0.1ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = ? LIMIT ? [["id", 37], ["LIMIT", 1]]
Item Exists (0.2ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = ? AND "items"."search_id" = 776 LIMIT ? [["collection_id", 37], ["LIMIT", 1]]
SQL (2.3ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["collection_id", 37], ["search_id", 776], ["created_at", "2018-06-18 12:47:58.540021"], ["updated_at", "2018-06-18 12:47:58.540021"]]
(6.2ms) commit transaction
Started GET "/sellers/16" for 127.0.0.1 at 2018-06-18 15:48:00 +0300
Processing by SellersController#show as HTML
Parameters: {"id"=>"16"}
Search Load (5.5ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = ? ORDER BY "searches"."id" ASC LIMIT ? [["term", "Bath Towel"], ["LIMIT", 1]]
(0.1ms) begin transaction
Search Exists (0.2ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = ? LIMIT ? [["term", "Bath Towel"], ["LIMIT", 1]]
SQL (1.4ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES (?, ?, ?) [["term", "Bath Towel"], ["created_at", "2018-06-18 12:48:01.250699"], ["updated_at", "2018-06-18 12:48:01.250699"]]
Seller Load (0.3ms) SELECT "sellers".* FROM "sellers" WHERE "sellers"."id" = ? LIMIT ? [["id", 16], ["LIMIT", 1]]
(9.6ms) commit transaction
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
Item Exists (0.3ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = ? AND "items"."collection_id" = 37 LIMIT ? [["search_id", 777], ["LIMIT", 1]]
(0.1ms) begin transaction
Collection Load (0.3ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = ? LIMIT ? [["id", 37], ["LIMIT", 1]]
Item Exists (0.2ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = ? AND "items"."search_id" = 777 LIMIT ? [["collection_id", 37], ["LIMIT", 1]]
SQL (1.0ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["collection_id", 37], ["search_id", 777], ["created_at", "2018-06-18 12:48:01.285942"], ["updated_at", "2018-06-18 12:48:01.285942"]]
Shop Load (8.8ms) SELECT "shops".* FROM "shops" WHERE "shops"."seller_id" = ? ORDER BY "shops"."id" DESC LIMIT ? [["seller_id", 16], ["LIMIT", 1]]
(40.2ms) commit transaction
Rendering sellers/show.html.erb within layouts/application
(0.4ms) SELECT COUNT(*) FROM "listings" WHERE "listings"."shop_id" = 12
Listing Load (0.4ms) SELECT "listings".* FROM "listings" WHERE "listings"."shop_id" = 12 LIMIT ? OFFSET ? [["LIMIT", 24], ["OFFSET", 0]]
Rendered sellers/show.html.erb within layouts/application (4.8ms)
Rendered layouts/_shim.html.erb (0.4ms)
Rendered layouts/_rails_default.html.erb (93.9ms)
Rendered layouts/_meta.html.erb (0.3ms)
Seller Exists (0.3ms) SELECT 1 AS one FROM "sellers" WHERE "sellers"."user_id" = ? LIMIT ? [["user_id", 1], ["LIMIT", 1]]
Seller Load (0.3ms) SELECT "sellers".* FROM "sellers" WHERE "sellers"."user_id" = ? ORDER BY "sellers"."id" DESC LIMIT ? [["user_id", 1], ["LIMIT", 1]]
Rendered layouts/_header.html.erb (5.5ms)
Seller Load (0.3ms) SELECT "sellers".* FROM "sellers" WHERE "sellers"."user_id" = ? [["user_id", 1]]
Shop Load (0.3ms) SELECT "shops".* FROM "shops" WHERE "shops"."seller_id" = ? ORDER BY "shops"."id" DESC LIMIT ? [["seller_id", 13], ["LIMIT", 1]]
CACHE Shop Load (0.0ms) SELECT "shops".* FROM "shops" WHERE "shops"."seller_id" = ? ORDER BY "shops"."id" DESC LIMIT ? [["seller_id", 16], ["LIMIT", 1]]
Rendered layouts/_sidebar.html.erb (7.3ms)
Rendered layouts/_end_sidebar.html.erb (0.4ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 212ms (Views: 120.5ms | ActiveRecord: 12.3ms)
After PG:
Started POST "/collections/1/item/choose.25" for 127.0.0.1 at 2018-06-18 16:56:12 +0300
Processing by ItemsController#choose as
Parameters: {"authenticity_token"=>"11+Rpf2qXEKhQ5vBJLWie2EBg1b2Dtrw8iIgoLwXyvake7+myDrVErcqTwNcPYAZ5xs+zFKXaJjwM7fTakXaoA==", "collection_id"=>"1"}
Collection Load (0.4ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Seller Load (0.6ms) SELECT "sellers".* FROM "sellers" WHERE "sellers"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Load (0.3ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT $2 [["id", 25], ["LIMIT", 1]]
(0.1ms) BEGIN
Collection Load (0.5ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Search Load (0.9ms) SELECT "searches".* FROM "searches" WHERE "searches"."id" = $1 LIMIT $2 [["id", 31], ["LIMIT", 1]]
Item Exists (0.7ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND ("items"."id" != $2) AND "items"."search_id" = 31 LIMIT $3 [["collection_id", 1], ["id", 25], ["LIMIT", 1]]
SQL (0.8ms) UPDATE "items" SET "chosen" = $1, "updated_at" = $2 WHERE "items"."id" = $3 [["chosen", "t"], ["updated_at", "2018-06-18 13:56:12.465841"], ["id", 25]]
(2.7ms) COMMIT
(0.4ms) BEGIN
SQL (0.6ms) UPDATE "collections" SET "keyword" = $1, "updated_at" = $2 WHERE "collections"."id" = $3 [["keyword", "Bookish Candles"], ["updated_at", "2018-06-18 13:56:12.473173"], ["id", 1]]
(1.5ms) COMMIT
Search Load (0.6ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "Bookish Candles"], ["LIMIT", 1]]
Listing Load (4.1ms) SELECT DISTINCT "listings".* FROM "listings" INNER JOIN "listings_searches" ON "listings"."id" = "listings_searches"."listing_id" WHERE "listings_searches"."search_id" = $1 ORDER BY "listings"."id" ASC LIMIT $2 [["search_id", 31], ["LIMIT", 3]]
Search Load (0.7ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "soy candles"], ["LIMIT", 1]]
(0.2ms) BEGIN
Search Exists (0.8ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "soy candles"], ["LIMIT", 1]]
SQL (0.6ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "soy candles"], ["created_at", "2018-06-18 13:56:12.491262"], ["updated_at", "2018-06-18 13:56:12.491262"]]
(2.3ms) COMMIT
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 46], ["LIMIT", 1]]
(0.2ms) BEGIN
Collection Load (1.3ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.7ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 46 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (1.0ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 46], ["created_at", "2018-06-18 13:56:12.504381"], ["updated_at", "2018-06-18 13:56:12.504381"]]
(1.1ms) COMMIT
Search Load (0.8ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "book candle"], ["LIMIT", 1]]
(0.2ms) BEGIN
Search Exists (0.5ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "book candle"], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "book candle"], ["created_at", "2018-06-18 13:56:14.812727"], ["updated_at", "2018-06-18 13:56:14.812727"]]
(2.8ms) COMMIT
Item Exists (0.7ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 47], ["LIMIT", 1]]
(0.1ms) BEGIN
Collection Load (1.0ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 47 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 47], ["created_at", "2018-06-18 13:56:14.825865"], ["updated_at", "2018-06-18 13:56:14.825865"]]
(1.4ms) COMMIT
Search Load (1.0ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "literary gifts"], ["LIMIT", 1]]
(0.4ms) BEGIN
Search Exists (0.7ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "literary gifts"], ["LIMIT", 1]]
SQL (0.7ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "literary gifts"], ["created_at", "2018-06-18 13:56:17.776368"], ["updated_at", "2018-06-18 13:56:17.776368"]]
(2.4ms) COMMIT
Item Exists (0.8ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 48], ["LIMIT", 1]]
(0.2ms) BEGIN
Collection Load (1.6ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.8ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 48 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (1.1ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 48], ["created_at", "2018-06-18 13:56:17.790424"], ["updated_at", "2018-06-18 13:56:17.790424"]]
(1.2ms) COMMIT
Search Load (0.7ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "book candles"], ["LIMIT", 1]]
(0.2ms) BEGIN
Search Exists (0.7ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "book candles"], ["LIMIT", 1]]
SQL (0.6ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "book candles"], ["created_at", "2018-06-18 13:56:20.872076"], ["updated_at", "2018-06-18 13:56:20.872076"]]
(1.4ms) COMMIT
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 49], ["LIMIT", 1]]
(0.1ms) BEGIN
Collection Load (0.6ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.7ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 49 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (0.8ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 49], ["created_at", "2018-06-18 13:56:20.883450"], ["updated_at", "2018-06-18 13:56:20.883450"]]
(2.3ms) COMMIT
Search Load (0.9ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "literary candles"], ["LIMIT", 1]]
(0.2ms) BEGIN
Search Exists (0.5ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "literary candles"], ["LIMIT", 1]]
SQL (0.8ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "literary candles"], ["created_at", "2018-06-18 13:56:22.523896"], ["updated_at", "2018-06-18 13:56:22.523896"]]
(1.4ms) COMMIT
Item Exists (0.7ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 50], ["LIMIT", 1]]
(0.2ms) BEGIN
Collection Load (0.7ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.9ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 50 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (0.9ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 50], ["created_at", "2018-06-18 13:56:22.536553"], ["updated_at", "2018-06-18 13:56:22.536553"]]
(2.2ms) COMMIT
Search Load (0.9ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "soy candle"], ["LIMIT", 1]]
Item Exists (0.7ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 18], ["LIMIT", 1]]
Search Load (0.6ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "8oz candles"], ["LIMIT", 1]]
(0.3ms) BEGIN
Search Exists (0.6ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "8oz candles"], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "8oz candles"], ["created_at", "2018-06-18 13:56:25.455733"], ["updated_at", "2018-06-18 13:56:25.455733"]]
(2.4ms) COMMIT
Item Exists (0.8ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 51], ["LIMIT", 1]]
(0.2ms) BEGIN
Collection Load (0.7ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 51 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (1.0ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 51], ["created_at", "2018-06-18 13:56:25.468089"], ["updated_at", "2018-06-18 13:56:25.468089"]]
(1.2ms) COMMIT
Search Load (0.7ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "handmade soy candles"], ["LIMIT", 1]]
(0.2ms) BEGIN
Search Exists (0.5ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "handmade soy candles"], ["LIMIT", 1]]
SQL (0.5ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "handmade soy candles"], ["created_at", "2018-06-18 13:56:27.445609"], ["updated_at", "2018-06-18 13:56:27.445609"]]
(1.3ms) COMMIT
Item Exists (1.1ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 52], ["LIMIT", 1]]
(0.2ms) BEGIN
Collection Load (0.5ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 52 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (0.6ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 52], ["created_at", "2018-06-18 13:56:27.457050"], ["updated_at", "2018-06-18 13:56:27.457050"]]
(2.5ms) COMMIT
Search Load (0.7ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "bookish candle"], ["LIMIT", 1]]
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 13], ["LIMIT", 1]]
Search Load (0.5ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "bookish candles"], ["LIMIT", 1]]
(0.2ms) BEGIN
Search Exists (0.5ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "bookish candles"], ["LIMIT", 1]]
SQL (1.0ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "bookish candles"], ["created_at", "2018-06-18 13:56:29.102682"], ["updated_at", "2018-06-18 13:56:29.102682"]]
(1.6ms) COMMIT
Item Exists (0.8ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 53], ["LIMIT", 1]]
(0.3ms) BEGIN
Collection Load (0.5ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.5ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 53 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (0.6ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 53], ["created_at", "2018-06-18 13:56:29.114736"], ["updated_at", "2018-06-18 13:56:29.114736"]]
(2.1ms) COMMIT
Search Load (1.0ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "personalized gifts"], ["LIMIT", 1]]
(0.2ms) BEGIN
Search Exists (0.8ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "personalized gifts"], ["LIMIT", 1]]
SQL (0.8ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "personalized gifts"], ["created_at", "2018-06-18 13:56:30.446268"], ["updated_at", "2018-06-18 13:56:30.446268"]]
(1.5ms) COMMIT
Item Exists (1.0ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 54], ["LIMIT", 1]]
(0.2ms) BEGIN
Collection Load (0.9ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.7ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 54 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (0.8ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 54], ["created_at", "2018-06-18 13:56:30.459078"], ["updated_at", "2018-06-18 13:56:30.459078"]]
(2.2ms) COMMIT
Search Load (0.7ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "personalized gift"], ["LIMIT", 1]]
(0.3ms) BEGIN
Search Exists (0.5ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "personalized gift"], ["LIMIT", 1]]
SQL (0.6ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "personalized gift"], ["created_at", "2018-06-18 13:56:32.460438"], ["updated_at", "2018-06-18 13:56:32.460438"]]
(2.5ms) COMMIT
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 55], ["LIMIT", 1]]
(0.2ms) BEGIN
Collection Load (0.7ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.7ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 55 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (0.7ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 55], ["created_at", "2018-06-18 13:56:32.474614"], ["updated_at", "2018-06-18 13:56:32.474614"]]
(1.1ms) COMMIT
Search Load (0.8ms) SELECT "searches".* FROM "searches" WHERE "searches"."term" = $1 ORDER BY "searches"."id" ASC LIMIT $2 [["term", "book smell"], ["LIMIT", 1]]
(0.5ms) BEGIN
Search Exists (0.8ms) SELECT 1 AS one FROM "searches" WHERE "searches"."term" = $1 LIMIT $2 [["term", "book smell"], ["LIMIT", 1]]
SQL (0.7ms) INSERT INTO "searches" ("term", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["term", "book smell"], ["created_at", "2018-06-18 13:56:34.573441"], ["updated_at", "2018-06-18 13:56:34.573441"]]
(1.3ms) COMMIT
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."search_id" = $1 AND "items"."collection_id" = 1 LIMIT $2 [["search_id", 56], ["LIMIT", 1]]
(0.1ms) BEGIN
Collection Load (1.4ms) SELECT "collections".* FROM "collections" WHERE "collections"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Item Exists (0.6ms) SELECT 1 AS one FROM "items" WHERE "items"."collection_id" = $1 AND "items"."search_id" = 56 LIMIT $2 [["collection_id", 1], ["LIMIT", 1]]
SQL (0.6ms) INSERT INTO "items" ("collection_id", "search_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["collection_id", 1], ["search_id", 56], ["created_at", "2018-06-18 13:56:34.585728"], ["updated_at", "2018-06-18 13:56:34.585728"]]
(2.5ms) COMMIT
Redirected to http://localhost:3000/collections/1
Completed 302 Found in 24422ms (ActiveRecord: 153.5ms)
The amount of items I'm inserting varies but even between many different examples, PG always takes longer, even though when looking at those two logs I posted, it seems like SQLite3 is taking longer per transaction.
Is this normal or is there something I can do to optimize it?
Thanks!
Looks like ActiveRecord is responding pretty quickly for both databases:
Sqlite: ActiveRecord: 12.3ms
Postgres: ActiveRecord: 153.5ms
While Postgres is certainly higher, I would think both of those response times would be acceptable, and that they would vary slightly each time you hit that action.
You do, however, have some sort of redirect taking place 302 Redirected to http://localhost:3000/collections/1 that seems to be adding a bunch of time to the request.
You might try making sure that there are no scripts on that page that are gumming up the works. Also, you might make sure that the indexes have been applied to your Postgres database, and also make sure your collections page doesn't have any n+1 queries.
Hard to say more with the limited information given in the question,
Despite providing the name, the SQL query clearly shows that it's not being passed properly. Rails console doesn't require any whitelist parameters as far as I'm aware, but I've included my controller as well.
Query in rails console: Profession.first.skills.create(name: 'rails')
Profession Load (0.5ms) SELECT "professions".* FROM "professions" ORDER BY "professions"."id" ASC LIMIT $1 [["LIMIT", 1]]
(0.2ms) BEGIN
Skill Exists (0.4ms) SELECT 1 AS one FROM "skills" WHERE "skills"."name" = $1 LIMIT $2 [["name", "twitter"], ["LIMIT", 1]]
SQL (1.0ms) INSERT INTO "skills" ("profession_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["profession_id", 1], ["created_at", "2017-12-18 12:22:11.154775"], ["updated_at", "2017-12-18 12:22:11.154775"]]
(0.1ms) ROLLBACK
Not sure what's going on here.
Validation errors are still working though...
Valid object:
:027 > Skill.new(name: "rails", profession: Profession.first).valid?
Profession Load (0.4ms) SELECT "professions".* FROM "professions" ORDER BY "professions"."id" ASC LIMIT $1 [["LIMIT", 1]]
Skill Exists (1.9ms) SELECT 1 AS one FROM "skills" WHERE "skills"."name" = $1 LIMIT $2 [["name", "twitter"], ["LIMIT", 1]]
=> true
Name being detected for validations:
:020 > Skill.create!(name: String.new, profession: Profession.first)
Profession Load (0.4ms) SELECT "professions".* FROM "professions" ORDER BY "professions"."id" ASC LIMIT $1 [["LIMIT", 1]]
(0.1ms) BEGIN
Skill Exists (0.4ms) SELECT 1 AS one FROM "skills" WHERE "skills"."name" = $1 LIMIT $2 [["name", ""], ["LIMIT", 1]]
(0.1ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
from (irb):20
Max Length:
:023 > Skill.create!(name: "sjadfkahskdfkjsahdfkjaskjdfkjhsdjkfhksajhfjksahasdljflasjdlfkjaskldjflkasjdklfjklasjdklfjlasjdflkjasklfjsdfhkjsahkjdfhjkasdhfkjhkj", profession: Profession.first)
Profession Load (0.4ms) SELECT "professions".* FROM "professions" ORDER BY "professions"."id" ASC LIMIT $1 [["LIMIT", 1]]
(0.2ms) BEGIN
Skill Exists (0.3ms) SELECT 1 AS one FROM "skills" WHERE "skills"."name" = $1 LIMIT $2 [["name", "sjadfkahskdfkjsahdfkjaskjdfkjhsdjkfhksajhfjksahasdljflasjdlfkjaskldjflkasjdklfjklasjdklfjlasjdflkjasklfjsdfhkjsahkjdfhjkasdhfkjhkj"], ["LIMIT", 1]]
(0.3ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Name is too long (maximum is 50 characters)
from (irb):23
ProfessionsController
class ProfessionsController < ApplicationController
def new
#profession = Profession.new
end
def create
#profession = Profession.find_by(name: profession_params[:name])
skill_params = get_nested_params(profession_params, :skills_attributes)
if #profession
# Save skill under existing profession
#skill = Skill.create(name: skill_params[:name], profession_id: #profession.id)
else
#profession = Profession.new(name: profession_params[:name])
if #profession.save {
saved_profession = Profession.find_by(name: profession_params[:name])
saved_profession.skills.create(name: "twitter")
# Skill.create(name: skill_params[:name], profession_id: Profession.find_by(name: profession_params[:name])).save!
}
end
end
respond_to do |format|
if #profession.save || #skill.save
format.js { render layout: false }
format.html { redirect_back fallback_location: root_path, notice: 'Profession was successfully created.' }
else
format.html { redirect_back fallback_location: root_path, notice: 'Skill was not created.' }
end
end
end
private
def profession_params
params.require(:profession).permit(:name,
skills_attributes: [:id,
:name,
:starting_date,
:profession_id,
:_destroy])
end
def get_nested_params parent_params, nested_params
nested_attrs = parent_params[nested_params]
nested_attrs[nested_attrs.keys[0]]
end
end
Updated:
Error from controller when using saved_profession.skills.create(name: "twitter")
Started POST "/professions" for 127.0.0.1 at 2017-12-19 01:50:50 +1300
Processing by ProfessionsController#create as JS
Parameters: {"utf8"=>"✓", "profession"=>{"name"=>"software", "skills_attributes"=>{"1513601431887"=>{"name"=>"rails", "_destroy"=>"false"}}}, "commit"=>"Create Profession"}
Profession Load (0.3ms) SELECT "professions".* FROM "professions" WHERE "professions"."name" = $1 LIMIT $2 [["name", "software"], ["LIMIT", 1]]
(0.1ms) BEGIN
Profession Exists (0.3ms) SELECT 1 AS one FROM "professions" WHERE "professions"."name" = $1 LIMIT $2 [["name", "software"], ["LIMIT", 1]]
SQL (0.3ms) INSERT INTO "professions" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["name", "software"], ["created_at", "2017-12-18 12:50:50.202270"], ["updated_at", "2017-12-18 12:50:50.202270"]]
Profession Load (0.2ms) SELECT "professions".* FROM "professions" WHERE "professions"."name" = $1 LIMIT $2 [["name", "software"], ["LIMIT", 1]]
Skill Exists (0.3ms) SELECT 1 AS one FROM "skills" WHERE "skills"."name" = $1 LIMIT $2 [["name", "twitter"], ["LIMIT", 1]]
SQL (0.7ms) INSERT INTO "skills" ("profession_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["profession_id", 4], ["created_at", "2017-12-18 12:50:50.205484"], ["updated_at", "2017-12-18 12:50:50.205484"]]
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 8ms (ActiveRecord: 2.2ms)
ActiveRecord::NotNullViolation (PG::NotNullViolation: ERROR: null value in column "name" violates not-null constraint
DETAIL: Failing row contains (11, null, 2017-12-14, 4, 2017-12-18 12:50:50.205484, 2017-12-18 12:50:50.205484, null).
: INSERT INTO "skills" ("profession_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"):
app/controllers/professions_controller.rb:21:in `block in create'
app/controllers/professions_controller.rb:18:in `create'
Try modifying your get_nested_params as:
def get_nested_params(parent_params, nested_params)
parent_params # For logging purpose only
# => {"name"=>"software", "skills_attributes"=>{"1513601431887"=>{"name"=>"rails", "_destroy"=>"false"}}}
nested_params # For logging purpose only
# => :skills_attributes
nested_attrs = parent_params[nested_params]
# => {"1513601431887"=>{"name"=>"rails", "_destroy"=>"false"}}
nested_attrs.values[0] # Return this
# => {"name"=>"rails", "_destroy"=>"false"}
end
I am unsure what is happening.
I have an attending button that user can click to attend an event.
when a user clicks on the attend button i get multiple post request in my terminal
this then displays the user attending the event 3 times
This only happens in the firefox browser
can one kindly tell me how to prevent this?
multiple post displayed in terminal:
Started POST "/attending_socials?social_id=new-members-night-out-west-london" for 127.0.0.1 at 2017-01-27 13:56:26 +0000
Processing by AttendingSocialsController#create as HTML
Parameters: {"authenticity_token"=>"G/9QyoKBaTTsivCKmcKAFO7RoPC6B7Lm6C4G7er2pGYIjYirJwcCUDXCrCoODk5tPl3cRMQUZM6fouQrLnvjRg==", "social_id"=>"new-members-night-out-west-london"}
Social Load (0.1ms) SELECT "socials".* FROM "socials" WHERE "socials"."slug" = ? ORDER BY "socials"."id" ASC LIMIT 1 [["slug", "new-members-night-out-west-london"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 20]]
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "attendances" ("attendable_id", "attendable_type", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["attendable_id", 11], ["attendable_type", "Social"], ["user_id", 20], ["created_at", "2017-01-27 13:56:26.450155"], ["updated_at", "2017-01-27 13:56:26.450155"]]
SQL (0.3ms) INSERT INTO "activities" ("parameters", "key", "owner_id", "owner_type", "trackable_id", "trackable_type", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["parameters", nil], ["key", "attendance.create"], ["owner_id", 20], ["owner_type", "User"], ["trackable_id", 156], ["trackable_type", "Attendance"], ["created_at", "2017-01-27 13:56:26.457301"], ["updated_at", "2017-01-27 13:56:26.457301"]]
(2.9ms) commit transaction
Redirected to http://localhost:3000/socials/new-members-night-out-west-london
Completed 302 Found in 19ms (ActiveRecord: 4.1ms)
Started POST "/attending_socials?social_id=new-members-night-out-west-london" for 127.0.0.1 at 2017-01-27 13:56:26 +0000
Processing by AttendingSocialsController#create as HTML
Parameters: {"authenticity_token"=>"G/9QyoKBaTTsivCKmcKAFO7RoPC6B7Lm6C4G7er2pGYIjYirJwcCUDXCrCoODk5tPl3cRMQUZM6fouQrLnvjRg==", "social_id"=>"new-members-night-out-west-london"}
Social Load (0.2ms) SELECT "socials".* FROM "socials" WHERE "socials"."slug" = ? ORDER BY "socials"."id" ASC LIMIT 1 [["slug", "new-members-night-out-west-london"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 20]]
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "attendances" ("attendable_id", "attendable_type", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["attendable_id", 11], ["attendable_type", "Social"], ["user_id", 20], ["created_at", "2017-01-27 13:56:26.488498"], ["updated_at", "2017-01-27 13:56:26.488498"]]
SQL (0.3ms) INSERT INTO "activities" ("parameters", "key", "owner_id", "owner_type", "trackable_id", "trackable_type", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["parameters", nil], ["key", "attendance.create"], ["owner_id", 20], ["owner_type", "User"], ["trackable_id", 157], ["trackable_type", "Attendance"], ["created_at", "2017-01-27 13:56:26.497129"], ["updated_at", "2017-01-27 13:56:26.497129"]]
(1.0ms) commit transaction
Redirected to http://localhost:3000/socials/new-members-night-out-west-london
Completed 302 Found in 19ms (ActiveRecord: 2.1ms)
Started POST "/attending_socials?social_id=new-members-night-out-west-london" for 127.0.0.1 at 2017-01-27 13:56:27 +0000
Processing by AttendingSocialsController#create as HTML
Parameters: {"authenticity_token"=>"G/9QyoKBaTTsivCKmcKAFO7RoPC6B7Lm6C4G7er2pGYIjYirJwcCUDXCrCoODk5tPl3cRMQUZM6fouQrLnvjRg==", "social_id"=>"new-members-night-out-west-london"}
Social Load (0.2ms) SELECT "socials".* FROM "socials" WHERE "socials"."slug" = ? ORDER BY "socials"."id" ASC LIMIT 1 [["slug", "new-members-night-out-west-london"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 20]]
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "attendances" ("attendable_id", "attendable_type", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["attendable_id", 11], ["attendable_type", "Social"], ["user_id", 20], ["created_at", "2017-01-27 13:56:27.510657"], ["updated_at", "2017-01-27 13:56:27.510657"]]
SQL (0.1ms) INSERT INTO "activities" ("parameters", "key", "owner_id", "owner_type", "trackable_id", "trackable_type", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["parameters", nil], ["key", "attendance.create"], ["owner_id", 20], ["owner_type", "User"], ["trackable_id", 158], ["trackable_type", "Attendance"], ["created_at", "2017-01-27 13:56:27.516075"], ["updated_at", "2017-01-27 13:56:27.516075"]]
(2.7ms) commit transaction
Redirected to http://localhost:3000/socials/new-members-night-out-west-london
Completed 302 Found in 16ms (ActiveRecord: 3.5ms)
route.rb:
resources :attending_socials, only: [:create, :destroy]
schema:
ActiveRecord::Schema.define(version: 20170121184409) do
create_table "attendances", force: :cascade do |t|
t.integer "attendable_id"
t.string "attendable_type"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "attendances", ["attendable_type", "attendable_id"], name: "index_attendances_on_attendable_type_and_attendable_id"
add_index "attendances", ["user_id"], name: "index_attendances_on_user_id"
end
attendance.rb
class Attendance < ActiveRecord::Base
belongs_to :user
belongs_to :attendable, polymorphic: true
end
attending_socials_controller.rb
class AttendingSocialsController < ApplicationController
before_action :set_social
def create
if Attendance.create(attendable: #social, user: current_user)
redirect_to :back, notice: 'Attending Social'
else
redirect_to :back, alert: 'Something went wrong...*sad panda*'
end
end
def destroy
Attendance.where(attendable_id: #social.id, user_id: current_user.id).first.destroy
redirect_to :back, notice: 'Not Attending Social'
end
private
def set_social
#social = Social.friendly.find(params[:social_id] || params[:id])
end
end
views file:
<div><%= link_to 'Attend testing', attending_socials_path(social_id: #social), method: :post %></div>
You could use this:
Attendance.find_or_create_by(attendable: #social, user: current_user)
given a user can attend each event only once