I can't fetch any rows from a Polymorphic association - ruby-on-rails

I've set up 2 polymorphic associations on a table, I have no problem adding to the table but I can't seem to retrieve the added information.
Here's what I'm doing:
Post
belongs_to :posted, polymorphic: true
belongs_to :received, polymorphic: true
User
has_many :posted_posts, class_name: 'Post', as: :posted
has_many :received_posts, class_name: 'Post', as: :received
Group
has_many :posted_posts, class_name: 'Post', as: :posted
has_many :received_posts, class_name: 'Post', as: :received
users_controller.rb
def post
authorize #user
#post = Post.new(post_params)
#post.received = #user
#post.posted = #current_user
if #post.save
respond_to do |format|
format.html {redirect_to root_url}
format.js
end
else
respond_to do |format|
format.html {redirect_to root_url}
format.js
end
end
end
Post seems to be fine, but I should be able to retrieve the information by using
<% #user.received_posts do |post| %>
<%= post.content %>
<% end %>
<% #user.posted_posts do |post| %>
<%= post.content %>
<% end %>
But neither of these work. What am I missing? It's not bringing up an error. In fact, the logs aren't showing anything.. it's like it's being ignored. Do I need a method somewhere that does some lifting? I though the rest is handled by rails..
Any help would be appreciated.
UPDATE - Logs
Started GET "/!/David" for 127.0.0.1 at 2014-08-18 12:02:32 +0100
Processing by UsersController#show as HTML
Parameters: {"username"=>"David"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE (username == 'David') LIMIT 1
(0.2ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."followable_id" = ? AND "follows"."followable_type" = ? AND "follows"."blocked" = 'f' [["followable_id", 3], ["followable_type", "User"]]
(0.2ms) SELECT COUNT(*) FROM "follows" WHERE "follows"."blocked" = 'f' AND "follows"."follower_id" = 2 AND "follows"."follower_type" = 'User' AND "follows"."followable_id" = 3 AND "follows"."followable_type" = 'User'
Rendered users/_links.html.erb (1.2ms)
Rendered users/show.html.erb within layouts/application (7.8ms)
Rendered layouts/_app_sidebar.html.erb (0.8ms)
Mailboxer::Conversation Load (0.6ms) SELECT DISTINCT "mailboxer_conversations".* FROM "mailboxer_conversations" INNER JOIN "mailboxer_notifications" ON "mailboxer_notifications"."conversation_id" = "mailboxer_conversations"."id" AND "mailboxer_notifications"."type" IN ('Mailboxer::Message') INNER JOIN "mailboxer_receipts" ON "mailboxer_receipts"."notification_id" = "mailboxer_notifications"."id" WHERE "mailboxer_notifications"."type" = 'Mailboxer::Message' AND "mailboxer_receipts"."receiver_id" = 2 AND "mailboxer_receipts"."receiver_type" = 'User' ORDER BY mailboxer_conversations.updated_at DESC
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 37]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 44]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 45]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 46]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 43]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 42]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 41]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 40]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 39]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 38]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 36]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.4ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 35]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 34]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 33]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.4ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 32]]
CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 31]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 30]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 29]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 28]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 27]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 26]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 25]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 24]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 23]]
CACHE (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 22]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 21]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 20]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 19]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 18]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 17]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 16]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 15]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 14]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 13]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 12]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 11]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 10]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 9]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 8]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 7]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 6]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 5]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.3ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 4]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 3]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 2]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Mailboxer::Message Load (0.2ms) SELECT "mailboxer_notifications".* FROM "mailboxer_notifications" WHERE "mailboxer_notifications"."type" IN ('Mailboxer::Message') AND "mailboxer_notifications"."conversation_id" = ? ORDER BY created_at DESC LIMIT 1 [["conversation_id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Rendered mailboxer/conversations/_conversation.html.erb (155.4ms)
Rendered layouts/_app_header.html.erb (159.5ms)
Rendered layouts/_messages.html.erb (0.1ms)
Completed 200 OK in 223ms (Views: 206.2ms | ActiveRecord: 13.5ms)

Turned out that you missed .each:
<% #user.posted_posts.each do |post| %>

Related

Rails ActiveAdmin Eliminating n+1 queries on custom attributes belong to another model

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

Upgraded from SQLite3 to PG - db is 100 times slower

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,

Rails: Rolify gem not updating the database

So here's is what I did:
Select the user:
>> user = User.find(337633)
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 337633], ["LIMIT", 1]]
#<User id: 337633, name: "Restaurant Guy", email: "restaurant#guy.com", mobile: "8000088888", image: "", password_digest: "$2a$10$H3TlQT1DdGOPQjSR7b1st.SVvAg5XiFidrfqzyqz0RW...", created_at: "2017-02-23 18:19:29", updated_at: "2017-02-23 18:21:43", uid: nil, provider: nil, verified_at: nil, location_id: nil>
Check number of roles:
>> user.roles.length
Role Load (1.0ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 [["user_id", 337633]]
1 #customer role
Grant the selected user a role on a specific resource:
>> user.grant :restaurant_admin, Restaurant.first
Restaurant Load (1.0ms) SELECT "restaurants".* FROM "restaurants" ORDER BY "restaurants"."id" ASC LIMIT $1 [["LIMIT", 1]]
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = $1 AND "roles"."resource_type" = $2 AND "roles"."resource_id" = $3 ORDER BY "roles"."id" ASC LIMIT $4 [["name", "restaurant_admin"], ["resource_type", "Restaurant"], ["resource_id", 1], ["LIMIT", 1]]
(1.0ms) BEGIN
(0.0ms) ROLLBACK
HABTM_Roles Load (0.0ms) SELECT "users_roles".* FROM "users_roles" WHERE "users_roles"."user_id" = $1 [["user_id", 337633]]
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
#<Role id: nil, name: "restaurant_admin", resource_type: "Restaurant", resource_id: 1, created_at: nil, updated_at: nil>
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = 4
>> user.save
(0.0ms) BEGIN
true
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."mobile" = $1 AND ("users"."id" != $2) LIMIT $3 [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) LIMIT $3 [["email", "restaurant#guy.com"], ["id", 337633], ["LIMIT", 1]]
(1.0ms) COMMIT
And save it:
>> user.save
(0.0ms) BEGIN
true
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."mobile" = $1 AND ("users"."id" != $2) LIMIT $3 [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) LIMIT $3 [["email", "restaurant#guy.com"], ["id", 337633], ["LIMIT", 1]]
(1.0ms) COMMIT
Reload from database:
>> reload!
Reloading...
true
Check if the saved value is present
>> user = User.find(337633)
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 337633], ["LIMIT", 1]]
#<User id: 337633, name: "Restaurant Guy", email: "restaurant#guy.com", mobile: "8000088888", image: "", password_digest: "$2a$10$H3TlQT1DdGOPQjSR7b1st.SVvAg5XiFidrfqzyqz0RW...", created_at: "2017-02-23 18:19:29", updated_at: "2017-02-23 18:21:43", uid: nil, provider: nil, verified_at: nil, location_id: nil>
>> user.roles.length
Role Load (0.0ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 [["user_id", 337633]]
1 #customer role :(
Why wasn't the update persisted?
What am I doing wrong?
UPDATE: Looks like there's no mistake on saving the updated parts but seems to be a bug in rolify gem when operated on Rails 5+
UPDATE 2: The aforementioned bug was resolved I believe, and I finally found what was causing the ROLLBACK -> failing to include resourcify.
Need to include resourcify on the resource that should be assigned to the role and the ROLLBACK was happening as the link was broken.
And I also found that user.save is useless here as the roles get into the db right after you grant them
Based on this SO answer try to call reload on the user object directly:
user.reload
It seems, that the rails console doesn't reload classes once they're referenced.
When you grant the selected user a role on a specific resource, it seems that there's no record add to users_roles.
>> user.grant :restaurant_admin, Restaurant.first
Restaurant Load (1.0ms) SELECT "restaurants".* FROM "restaurants" ORDER BY "restaurants"."id" ASC LIMIT $1 [["LIMIT", 1]]
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = $1 AND "roles"."resource_type" = $2 AND "roles"."resource_id" = $3 ORDER BY "roles"."id" ASC LIMIT $4 [["name", "restaurant_admin"], ["resource_type", "Restaurant"], ["resource_id", 1], ["LIMIT", 1]]
(1.0ms) BEGIN
(0.0ms) ROLLBACK
HABTM_Roles Load (0.0ms) SELECT "users_roles".* FROM "users_roles" WHERE "users_roles"."user_id" = $1 [["user_id", 337633]]
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = $1 LIMIT $2 [["id", 4], ["LIMIT", 1]]
#<Role id: nil, name: "restaurant_admin", resource_type: "Restaurant", resource_id: 1, created_at: nil, updated_at: nil>
Role Load (0.0ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = 4
>> user.save
(0.0ms) BEGIN
true
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."mobile" = $1 AND ("users"."id" != $2) LIMIT $3 [["mobile", "8000088888"], ["id", 337633], ["LIMIT", 1]]
User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) LIMIT $3 [["email", "restaurant#guy.com"], ["id", 337633], ["LIMIT", 1]]
(1.0ms) COMMIT
it was ROLLBACKed.
so i think there's something wrong with the method user#grant

Rails 4: Double request/response/select/insert

in my rails 4 app I noticed, that when I create new comment, every request/response are doubles. anybody know why?
I'm using ajax
this is my logs:
Started POST "/comments" for 127.0.0.1 at 2015-03-25 21:07:06 +0400
Started POST "/comments" for 127.0.0.1 at 2015-03-25 21:07:06 +0400
Processing by CommentsController#create as JS
Processing by CommentsController#create as JS
Parameters: {"utf8"=>"✓", "recipe_id"=>"1", "comment"=>"llkl;kl;k;l", "commit"=>"Add Comment"}
Parameters: {"utf8"=>"✓", "recipe_id"=>"1", "comment"=>"llkl;kl;k;l", "commit"=>"Add Comment"}
Recipe Load (0.1ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT 1 [["id", 1]]
Recipe Load (0.1ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "comments" ("commentable_id", "commentable_type", "comment", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["commentable_id", 1], ["commentable_type", "Recipe"], ["comment", "llkl;kl;k;l"], ["user_id", 1], ["created_at", "2015-03-25 17:07:06.221648"], ["updated_at", "2015-03-25 17:07:06.221648"]]
SQL (0.2ms) INSERT INTO "comments" ("commentable_id", "commentable_type", "comment", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["commentable_id", 1], ["commentable_type", "Recipe"], ["comment", "llkl;kl;k;l"], ["user_id", 1], ["created_at", "2015-03-25 17:07:06.221648"], ["updated_at", "2015-03-25 17:07:06.221648"]]
(17.8ms) commit transaction
(17.8ms) commit transaction
Comment Load (2.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = ? AND "comments"."commentable_type" = ? ORDER BY created_at DESC LIMIT 15 OFFSET 0 [["commentable_id", 1], ["commentable_type", "Recipe"]]
Comment Load (2.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = ? AND "comments"."commentable_type" = ? ORDER BY created_at DESC LIMIT 15 OFFSET 0 [["commentable_id", 1], ["commentable_type", "Recipe"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Rendered recipes/_comments.html.erb (6.2ms)
Rendered recipes/_comments.html.erb (6.2ms)
Rendered comments/create.js.erb (15.3ms)
Rendered comments/create.js.erb (15.3ms)
Completed 200 OK in 54ms (Views: 16.4ms | ActiveRecord: 20.3ms)
Completed 200 OK in 54ms (Views: 16.4ms | ActiveRecord: 20.3ms)

No error is shown but return result is empty in index

i'm trying to create a Result which belongs to Generator. In order to create the Result, I did the code in Generator show.html.erb and render the form ( of Result ) in the show.html.erb of Generator. However, Result method is not called when i click the submt button. How can i solve this ? There are no errors shown either so i do not know where goes wrong. When i type Result.all in console, no records were in the db as well. I followed the way of associating 2nd object with the 1st but still can't get them right. Please help me .
Log ( no idea why it's so long. Definately there's something gone wrong)
Started GET "/users/1/generators/new" for 127.0.0.1 at 2013-12-13 23:36:51 +0800
Processing by GeneratorsController#new as HTML
Parameters: {"user_id"=>"1"}
[1m[35mUser Load (2.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
Rendered generators/_form.html.erb (77.0ms)
Rendered generators/new.html.erb within layouts/application (82.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Completed 200 OK in 256ms (Views: 179.0ms | ActiveRecord: 15.0ms)
Started POST "/users/1/generators" for 127.0.0.1 at 2013-12-13 23:36:56 +0800
Processing by GeneratorsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SBzLTWzzkziEGPDL7BZFnxdKuVeBJXQXUpKta3k5erQ=", "choice"=>"Randomly", "generator"=>{"primer_length"=>"10"}, "no_A"=>"", "no_T"=>"", "no_G"=>"", "no_C"=>"", "user_seq"=>"", "result_choice"=>"Yes", "commit"=>"Generate", "user_id"=>"1"}
[1m[36mUser Load (2.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36m (2.0ms)[0m [1mBEGIN[0m
[1m[35mSQL (3.0ms)[0m INSERT INTO "generators" ("choice", "created_at", "f_primer", "melting_temp", "primer_length", "r_primer", "random_primer_generated", "result_choice", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["choice", "Randomly"], ["created_at", Fri, 13 Dec 2013 23:36:56 MYT +08:00], ["f_primer", "GTGCTAAAAT"], ["melting_temp", 26.0], ["primer_length", 10], ["r_primer", "ATTTTAGCAC"], ["random_primer_generated", "GTGCTAAAAT"], ["result_choice", "Yes"], ["updated_at", Fri, 13 Dec 2013 23:36:56 MYT +08:00], ["user_id", 1]]
[1m[36m (103.0ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/users/1/generators/5
Completed 302 Found in 136ms (ActiveRecord: 110.0ms)
Started GET "/users/1/generators/5" for 127.0.0.1 at 2013-12-13 23:36:56 +0800
Processing by GeneratorsController#show as HTML
Parameters: {"user_id"=>"1", "id"=>"5"}
[1m[35mUser Load (2.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mGenerator Load (2.0ms)[0m [1mSELECT "generators".* FROM "generators" WHERE "generators"."id" = $1 LIMIT 1[0m [["id", "5"]]
[1m[35mResult Load (2.0ms)[0m SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1 [["generator_id", 5]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
Rendered results/_form.html.erb (79.0ms)
Rendered generators/show.html.erb within layouts/application (85.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Completed 200 OK in 140ms (Views: 119.0ms | ActiveRecord: 14.0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/generators.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/identities.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/results.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/sessions.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/welcome.css?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/generators.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/identities.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/registrations.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:57 +0800
Started GET "/assets/results.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800
Started GET "/assets/sessions.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800
Started GET "/assets/welcome.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-12-13 23:36:58 +0800
Started PUT "/users/1/generators/5?generator_id=5" for 127.0.0.1 at 2013-12-13 23:37:08 +0800
Processing by GeneratorsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"SBzLTWzzkziEGPDL7BZFnxdKuVeBJXQXUpKta3k5erQ=", "ncbi_ref_seq"=>"", "genome_seq"=>"TGATGAACATCATGATGAGGTGATGACATCACATCATTGACTGATGCATCATGATG", "commit"=>"Analyze", "generator_id"=>"5", "user_id"=>"1", "id"=>"5"}
[1m[36mUser Load (2.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mGenerator Load (2.0ms)[0m SELECT "generators".* FROM "generators" WHERE "generators"."id" = $1 LIMIT 1 [["id", "5"]]
[1m[36m (2.0ms)[0m [1mBEGIN[0m
[1m[35m (2.0ms)[0m COMMIT
[1m[36m (1.0ms)[0m [1mBEGIN[0m
[1m[35m (2.0ms)[0m COMMIT
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
Redirected to http://localhost:3000/users/1/generators.5?id=5
Completed 302 Found in 27ms (ActiveRecord: 11.0ms)
Started GET "/users/1/generators.5?id=5" for 127.0.0.1 at 2013-12-13 23:37:08 +0800
Processing by GeneratorsController#index as
Parameters: {"id"=>"5", "user_id"=>"1"}
[1m[35mUser Load (2.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mGenerator Load (2.0ms)[0m SELECT "generators".* FROM "generators" WHERE "generators"."user_id" = $1 [["user_id", 1]]
[1m[36mResult Load (1.0ms)[0m [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m [["generator_id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mResult Load (2.0ms)[0m SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1 [["generator_id", 2]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mResult Load (2.0ms)[0m [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m [["generator_id", 3]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mResult Load (2.0ms)[0m SELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1 [["generator_id", 4]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mResult Load (2.0ms)[0m [1mSELECT "results".* FROM "results" WHERE "results"."generator_id" = $1 ORDER BY "results"."id" ASC LIMIT 1[0m [["generator_id", 5]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
Rendered generators/index.html.erb within layouts/application (61.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Completed 200 OK in 128ms (Views: 111.0ms | ActiveRecord: 13.0ms)
Generator controller
def show
end
# GET /generators/new
def new
#generator=Generator.new(params[:generator])
end
def index
##generators = current_user.generators
##generators = #generators.order("created_at DESC")
end
def create
#generator = current_user.generators.build(generator_params)
#generator.choice = params[:choice]
if params[:choice] == 'Randomly'
#generator.random_generate(generator_params)
elsif params[:choice] == 'Specified ATGC'
#generator.specified_ATGC(params[:no_A],params[:no_T],params[:no_G],params[:no_C])
elsif params[:choice] == 'Seating'
#generator.seating(params[:user_seq])
end
#generator.result_choice=params[:result_choice]
#generator.save
respond_to do |format|
if #generator.result_choice == 'Yes'
format.html { redirect_to(:action =>"show",:id =>#generator.id )}
else
format.html { redirect_to(user_generators_path(#generator,:user_id => current_user.id,:generator_id=>#generator.id) ) }
end
end
end
# PATCH/PUT /generators/1
# PATCH/PUT /generators/1.json
def update
respond_to do |format|
if #generator.update(params[:generator])
#generator.save
format.html { redirect_to(user_generators_path(#generator,:user_id => current_user.id,:id=>#generator.id) ) }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
Result controller
def new
end
# GET /results/1/edit
# POST /results
# POST /results.json TGATGAACATCATGATGAGGTGATGACATCACATCATTGACTGATGCATCATGATG
def create
#result = #generator.build_result(result_params)
#generator=#result.generate_result(result_params)
#generator.result.save
redirect_to user_generators_path
end
Result Form that was rendered in Generator show.html.erb
<%= form_for(#generator.build_result(params[:result]),:url =>user_generator_path(#generator,:user_id => current_user.id, :generator_id => #generator.id),:html =>{:method=>:put}) do |f| %>
<script>
$(document).ready(function(){
$("#accession_number").click(function(){
$("#FASTA").hide();
$("#highlight").hide();
})
$("#FASTA").click(function(){
$("#accession_number").hide();
$("#highlight").hide();
})
$("#reset").click(function(){
$("#accession_number").show();
$("#FASTA").show();
$("#highlight").show();
})
});
</script>
<font size ="6">*** <u>Choose ONE of the input methods</u> *** </font>
<p id="accession_number">
<label><strong><font size ="3">NCBI accession number:</font></strong></label><br />
<%= text_field_tag(:ncbi_ref_seq,nil,placeholder: 'Accession Number')%>
</p>
<p id="highlight"><font size ="5">
OR
</font></p>
<p id="FASTA">
<strong><font size ="3">Paste your sequence here: ( FASTA format ) :</font></strong>
<%= text_area_tag(:genome_seq,nil,size: "50x10",placeholder: 'Input your sequence here')%>
</p>
<input type="reset" value="Reset" id="reset">
<%= submit_tag 'Analyze' %>
<% end %>
Form for Generator ( i need to use tag else if i use f.button , i get empty result)
<html>
<script>
$(document).ready(function() {
var total = 0;
$.each($(':input'), function(){
total += parseInt($(this).val(), 10); //use radix 10 to ensure correct parseInt val
});
$("span").text(i);
});
</script>
<body >
<%= form_for(#generator, :url => user_generators_path(:user_id => current_user.id, :id => #generator.id)) do |f| %>
<% if #generator.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#generator.errors.count, "error") %> prohibited this generator from being saved:</h2>
<ul>
<% #generator.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% else %>
<fieldset class ="primer">
<legend><strong>Method Use :</strong></legend><br>
<h3 align="left"><font size ="5"><b>Step 1: <u>Choose only ONE of the methods</u></font></b></h3>
<table class="p_gen">
<thead>
<tr>
<th class="method1"><label>Randomly</label><br />
<%= radio_button_tag(:choice, 'Randomly',checked:true )%>
<p> ( Generate a primer randomly )</p></th>
<th class="method2"><label>Specified ATGC</label><br />
<%= radio_button_tag(:choice,'Specified ATGC')%>
<p> ( Generate a primer with number of A,T,G and C )</p></th>
<th class="method3"><label>Seating</label><br />
<%= radio_button_tag(:choice,'Seating')%>
<p> ( Generate a primer according to your preference )</p></th>
</tr>
</thead>
<tbody>
<tr>
<td class="method1">
<p> Input length of the primer you want : </p>
<label>Primer Length :</label>
<%= f.number_field(:primer_length , min: 6 , max: 35)%>
</td>
<td class="method2">
<p>Input the number of each base the primer should have</p>
<label>Number of A :</label>
<%= number_field_tag :no_A,nil, :min=>0, :max=>35 %><br />
<label>Number of T :</label>
<%= number_field_tag :no_T,nil, :min=>0, :max=>35 %><br />
<label>Number of G :</label>
<%= number_field_tag :no_G,nil, :min=>0, :max=>35 %><br />
<label>Number of C :</label>
<%= number_field_tag :no_C,nil, :min=>0, :max=>35 %><br />
Total bases:<span></span>
</td>
<td class="method3">
<p> Input your preference sequence (only IUPAC nucleotide).</p>
<p><b><u>IUPAC Nucleotide :</u></b></p>
<p>A,T,G,C,R,Y,S,W,K,M,B,D,H,V,N </p>
Example: <br />
Preference primer = TAGGCT<b>N</b>TTA<b>N</b>GAC<b>N</b> <br />
N = Any base ( A/ T / G / C) <br /><br />
<label>Desired sequence :</label><br>
<%= text_field_tag(:user_seq,nil,:maxlength => 35)%>
</td>
</tr>
</tbody>
</table>
</fieldset>
<br>
<fieldset class ="sample">
<h4><font size="5"><b>Step 2: <u>Choose 'Yes' if you want to input reference sequence for Binding-time analysis </font></u></b></h4>
<legend><strong>Do you have NCBI data to extract / FASTA file to input?</strong></legend><br>
<label>Yes</label>
<%= radio_button_tag(:result_choice,'Yes')%>
<label>No</label>
<%= radio_button_tag(:result_choice,'No')%>
<br>
</fieldset>
<br><div class = "button">
<%=f.submit("Generate", :class => "Gbutton_class") %>
</div>
<%end %>
<%end%>
</body>
</html>
routes.rb
Project::Application.routes.draw do
get "/signout" => "sessions#destroy", :as => :signout
root :to => 'welcome#index'
get '/auth/:provider/callback' => 'sessions#create'
post '/auth/identity/callback' => 'sessions#create'
resources :users do
resources :generators
resources :results
end
resources :identities
I'm not entirely clear why there are two Generator forms (perhaps I'm missing something), but in some places you're using the singular user: user_generators_path where in others you're using users_generators_path (note the plural users). Check which is correct via rake routes, and fix any incorrect entries.

Resources