I'm using sitemap_generator gem to auto-generate the sitemap. It's already configured, tested and it's worked locally, but when I'm in production on my Dokku container, trying to run dokku run tradfood rake -s sitemap:refresh, following lines appear :
City Load (0.7ms) SELECT "cities".* FROM "cities" ORDER BY "cities"."id" ASC LIMIT $1 [["LIMIT", 1000]]
Recipe Load (2.5ms) SELECT "recipes".* FROM "recipes" ORDER BY "recipes"."id" ASC LIMIT $1 [["LIMIT", 1000]]
City Load (8.1ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
City Load (1.3ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
City Load (0.3ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
City Load (0.2ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
City Load (0.2ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
City Load (0.3ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
City Load (0.2ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
City Load (0.2ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
City Load (0.2ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
But nothing has been generated : when I run dokku run tradfood ls -all public, sitemap.xml stay to oct. 21.
My Dokku persistent storage still works with my image uploader on production (it record well new images, using carrierwave).
Here my sitemap.rb configuration :
# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "https://tradfood.fr"
SitemapGenerator::Sitemap.create(:compress => false) do
# Put links creation logic here.
#
# The root path '/' and sitemap index file are added automatically for you.
# Links are added to the Sitemap in the order they are specified.
#
# Usage: add(path, options={})
# (default options are used if you don't specify)
#
# Defaults: :priority => 0.5, :changefreq => 'weekly',
# :lastmod => Time.now, :host => default_host
#
# Examples:
#
# Add '/articles'
#
# add articles_path, :priority => 0.7, :changefreq => 'daily'
#
# Add all articles:
#
# Article.find_each do |article|
# add article_path(article), :lastmod => article.updated_at
# end
add '/a-propos', :changefreq => 'monthly'
City.find_each do |city|
add city_path(city.name.parameterize), :lastmod => city.updated_at, :changefreq => 'weekly', :priority => 0.8
end
Recipe.find_each do |recipe|
add recipe_path(recipe.city.name.parameterize, recipe.name.parameterize), :lastmod => recipe.updated_at, :changefreq => 'daily'
end
end
That gist no longer exists. Mind using gist.github.com or pasting it in something native to stackoverflow instead?
I'm guessing:
You are not writing to persistent storage
You are not reading the file from persistent storage
Related
In my app I have overwritten current_user devise method a bit. The idea is that if certain cookie is present method check the organization by the id inside that cookie and returns owner of this organization instead of regular user:
def current_user
user = warden.authenticate(scope: :user)
return nil if user.nil?
if user.admin? && cookies.key?('mock_admin_login')
organization = Organization.includes(:creator).find(cookies.encrypted[:mock_admin_login])
return organization.creator
end
user
end
Everything works correct but when I take a look at my console I noticed that Organization query is performed multiple times:
CACHE Organization Load (0.5ms) SELECT "organizations".* FROM
"organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 9],
["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user' CACHE Organization Load (0.9ms) SELECT
"organizations".* FROM "organizations" WHERE "organizations"."id" = $1
LIMIT $2 [["id", 9], ["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user' CACHE Organization Load (0.7ms) SELECT
"organizations".* FROM "organizations" WHERE "organizations"."id" = $1
LIMIT $2 [["id", 9], ["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user' CACHE Organization Load (0.3ms) SELECT
"organizations".* FROM "organizations" WHERE "organizations"."id" = $1
LIMIT $2 [["id", 9], ["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user' CACHE Organization Load (0.4ms) SELECT
"organizations".* FROM "organizations" WHERE "organizations"."id" = $1
LIMIT $2 [["id", 9], ["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user' CACHE Organization Load (2.0ms) SELECT
"organizations".* FROM "organizations" WHERE "organizations"."id" = $1
LIMIT $2 [["id", 9], ["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (4.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user' CACHE Organization Load (0.4ms) SELECT
"organizations".* FROM "organizations" WHERE "organizations"."id" = $1
LIMIT $2 [["id", 9], ["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user' CACHE Organization Load (42.8ms) SELECT
"organizations".* FROM "organizations" WHERE "organizations"."id" = $1
LIMIT $2 [["id", 9], ["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user' CACHE Organization Load (4.5ms) SELECT
"organizations".* FROM "organizations" WHERE "organizations"."id" = $1
LIMIT $2 [["id", 9], ["LIMIT", 1]] ↳
app/controllers/concerns/current_methods_overwritten.rb:11:in
current_user' CACHE User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 [["id", 10]] ↳ app/controllers/concerns/current_methods_overwritten.rb:11:in current_user'
Although It might seem like a not big deal but server spends additional 30-40ms to perform this action every time when current_user method is called. Why this query is called so many times instead of one and how can I fix it?
You need to memoize the result so that its not reevaluated every time you call current_user.
If you look at the helper that devise generates you can see that it does just that:
def current_#{mapping}
#current_#{mapping} ||= warden.authenticate(scope: :#{mapping})
end
If you want to fix your existing method you want to make sure to memoize the DB calls:
def current_user
#current_user ||= warden.authenticate(scope: :#{mapping})
if #current_user&.admin? && cookies.key?('mock_admin_login')
#current_org || = Organization.includes(:creator)
.find(cookies.encrypted[:mock_admin_login])
#current_user = #current_org.creator
end
#current_user
end
But you really should implement this as a custom Warden strategy instead.
I am building an app in rails with an admin interface using the activeadmin gem. I have a User model, an Organization Model and an Account Model (polymorphic). A same user can have different types of accounts (Trainer, Team or Athlete) and an organization has many accounts (they are members of this organization).
Only the admin can create organizations and send invitations to join them. I have an OrganizationInvitation model in active admin and I am trying to use devise_invitable.
ActiveAdmin.register OrganizationInvitation do
permit_params :organization_id, :email, :status
form do |f|
f.semantic_errors
f.inputs do
f.input :organization, collection: Organization.all.map { |organization| [organization.name, organization.id] }
f.input :email
end
f.actions
end
controller do
def create
#organization_invitation = OrganizationInvitation.new(organization_invitation_params)
#email = organization_invitation_params[:email]
#user = User.invite!(email: #email)
if #organization_invitation.save
redirect_to admin_organization_invitations_path, notice: 'Invitation successfully sent'
else
redirect_to admin_organization_invitations_path, alert: 'Something went wrong'
end
end
private
def organization_invitation_params
params.require(:organization_invitation).permit(
:organization_id,
:email
)
end
end
end
There will be different small issues to handle the different types of accounts, the case in which a user already has an account etc but for now I am just trying to send an email to invite a new user to an organization. When I test it, the user gets created in the database but apparently no email gets sent. I did the basic setup for devise_invitable and I added this line in my development config file but it still does not work.
config.action_mailer.perform_deliveries = true
I never used these 2 gems before and feel I am doing sth wrong but do not know what. Thank you in advance!
Edit: Here are the logs as well
Started GET "/admin/organization_invitations" for 127.0.0.1 at 2019-03-21 16:10:44 +0000
Processing by Admin::OrganizationInvitationsController#index as HTML
AdminUser Load (3.3ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/index.html.arb
(1.1ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE (0.0ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
(0.4ms) SELECT COUNT(*) FROM "organization_invitations"
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE (0.0ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
OrganizationInvitation Load (0.8ms) SELECT "organization_invitations".* FROM "organization_invitations" ORDER BY "organization_invitations"."id" desc LIMIT $1 OFFSET $2 [["LIMIT", 30], ["OFFSET", 0]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Organization Load (1.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Organization Load (1.8ms) SELECT "organizations".* FROM "organizations"
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/index.html.arb (194.2ms)
Completed 200 OK in 244ms (Views: 224.8ms | ActiveRecord: 13.5ms)
Started GET "/admin/organization_invitations/new" for 127.0.0.1 at 2019-03-21 16:10:46 +0000
Processing by Admin::OrganizationInvitationsController#new as HTML
AdminUser Load (0.5ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/new.html.arb
Organization Load (0.3ms) SELECT "organizations".* FROM "organizations"
↳ app/admin/organization_invitations.rb:8
Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/new.html.arb (105.3ms)
Completed 200 OK in 169ms (Views: 161.7ms | ActiveRecord: 0.8ms)
Started POST "/admin/organization_invitations" for 127.0.0.1 at 2019-03-21 16:10:51 +0000
Processing by Admin::OrganizationInvitationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XkxCRz+/BsvXAmyKUZk4UZhjtE1AcrhOnCxheNQGPQVP+j6tf+rZYVqqiU+CxtYR57pPShgjJdknG9PmiNtGOA==", "organization_invitation"=>{"organization_id"=>"1", "email"=>"myaddress#gmail.com"}, "commit"=>"Create Organization invitation"}
AdminUser Load (2.0ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
User Load (1.4ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["email", "antoinecquellier#gmail.com"], ["LIMIT", 1]]
↳ app/admin/organization_invitations.rb:18
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."invitation_token" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["invitation_token", "c94d3b1e55b5cf39eba111085ec94eb1dcccca321308b87017a7f08b1634a3df"], ["LIMIT", 1]]
↳ app/admin/organization_invitations.rb:18
(0.3ms) BEGIN
↳ app/admin/organization_invitations.rb:18
User Update (1.6ms) UPDATE "users" SET "invitation_token" = $1, "invitation_created_at" = $2, "invitation_sent_at" = $3, "updated_at" = $4 WHERE "users"."id" = $5 [["invitation_token", "c94d3b1e55b5cf39eba111085ec94eb1dcccca321308b87017a7f08b1634a3df"], ["invitation_created_at", "2019-03-21 16:10:51.702522"], ["invitation_sent_at", "2019-03-21 16:10:51.702522"], ["updated_at", "2019-03-21 16:10:51.704500"], ["id", 1]]
↳ app/admin/organization_invitations.rb:18
(4.7ms) COMMIT
↳ app/admin/organization_invitations.rb:18
Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise_invitable-2.0.0/app/views/devise/mailer/invitation_instructions.html.erb
Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise_invitable-2.0.0/app/views/devise/mailer/invitation_instructions.html.erb (2.4ms)
Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise_invitable-2.0.0/app/views/devise/mailer/invitation_instructions.text.erb
Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/devise_invitable-2.0.0/app/views/devise/mailer/invitation_instructions.text.erb (5.2ms)
Devise::Mailer#invitation_instructions: processed outbound mail in 291.5ms
Sent mail to antoinecquellier#gmail.com (10.4ms)
Date: Thu, 21 Mar 2019 16:10:52 +0000
From: please-change-me-at-config-initializers-devise#example.com
Reply-To: please-change-me-at-config-initializers-devise#example.com
To: antoinecquellier#gmail.com
Message-ID: <5c93b78c2ad5_9da3fc49695b254502d0#MBP-de-Antoine.mail>
Subject: Invitation instructions
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_5c93b78c751_9da3fc49695b25450147";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_5c93b78c751_9da3fc49695b25450147
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hello antoinecquellier#gmail.com
Someone has invited you to http://localhost:3000/, you can accept it through the link below.
http://localhost:3000/users/invitation/accept?invitation_token=LtbgEbDe4ac63rs7oeC3
If you don't want to accept the invitation, please ignore this email. Your account won't be created until you access the link above and set your password.
----==_mimepart_5c93b78c751_9da3fc49695b25450147
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Hello antoinecquellier#gmail.com</p>
<p>Someone has invited you to http://localhost:3000/, you can accept it through the link below.</p>
<p>Accept invitation</p>
<p>If you don't want to accept the invitation, please ignore this email. Your account won't be created until you access the link above and set your password.</p>
----==_mimepart_5c93b78c751_9da3fc49695b25450147--
(0.2ms) BEGIN
↳ app/admin/organization_invitations.rb:20
Organization Load (0.4ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/admin/organization_invitations.rb:20
OrganizationInvitation Create (1.5ms) INSERT INTO "organization_invitations" ("organization_id", "email", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["organization_id", 1], ["email", "antoinecquellier#gmail.com"], ["created_at", "2019-03-21 16:10:52.023105"], ["updated_at", "2019-03-21 16:10:52.023105"]]
↳ app/admin/organization_invitations.rb:20
(1.6ms) COMMIT
↳ app/admin/organization_invitations.rb:20
Redirected to http://localhost:3000/admin/organization_invitations
Completed 302 Found in 345ms (ActiveRecord: 15.0ms)
Started GET "/admin/organization_invitations" for 127.0.0.1 at 2019-03-21 16:10:52 +0000
Processing by Admin::OrganizationInvitationsController#index as HTML
AdminUser Load (0.5ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Rendering /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/index.html.arb
(0.6ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE (0.0ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
(0.5ms) SELECT COUNT(*) FROM "organization_invitations"
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE (0.0ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "organization_invitations" LIMIT $1 OFFSET $2) subquery_for_count [["LIMIT", 30], ["OFFSET", 0]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
OrganizationInvitation Load (0.5ms) SELECT "organization_invitations".* FROM "organization_invitations" ORDER BY "organization_invitations"."id" desc LIMIT $1 OFFSET $2 [["LIMIT", 30], ["OFFSET", 0]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Organization Load (0.5ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.1ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.1ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
CACHE Organization Load (0.0ms) SELECT "organizations".* FROM "organizations" WHERE "organizations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Organization Load (0.6ms) SELECT "organizations".* FROM "organizations"
↳ /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/log_subscriber.rb:98
Rendered /Users/antoinequellier/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activeadmin-1.4.3/app/views/active_admin/resource/index.html.arb (204.8ms)
Completed 200 OK in 240ms (Views: 233.9ms | ActiveRecord: 3.6ms)
Regarding the mailer config, I'm not sure what you're refering to. Should not this be handled in the DeviseInvitable config?
Config in development.rb
config.action_mailer.default_url_options = { host: 'http://localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: "587",
domain: "gmail.com",
user_name: "myaddress#gmail.com",
password: "password",
authentication: "plain",
enable_starttls_auto: true
}
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
config.action_mailer.perform_caching = false
In my Rails app I have the N+1 problem where I'm making extra call(s) to the database to get associated data over and over again, specially when logging impressions per model.
For example:
Started GET "/" for 127.0.0.1 at 2018-12-02 16:21:05 -0500
Processing by JobsController#index as HTML
Job Load (4.4ms) SELECT "jobs".* FROM "jobs" WHERE "jobs"."published_at" IS NOT NULL ORDER BY "jobs"."published_at" DESC LIMIT $1 OFFSET $2 [["LIMIT", 30], ["OFFSET", 0]]
↳ app/controllers/jobs_controller.rb:22
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 13], ["LIMIT", 1]]
↳ app/controllers/jobs_controller.rb:24
Impression Exists (1.3ms) SELECT 1 AS one FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2 AND "impressions"."session_hash" = $3 LIMIT $4 [["impressionable_id", 705], ["impressionable_type", "Job"], ["session_hash", "d80d52dd401011a626d600167140e49f"], ["LIMIT", 1]]
↳ app/controllers/jobs_controller.rb:24
Impression Exists (0.6ms) SELECT 1 AS one FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2 AND "impressions"."session_hash" = $3 LIMIT $4 [["impressionable_id", 704], ["impressionable_type", "Job"], ["session_hash", "d80d52dd401011a626d600167140e49f"], ["LIMIT", 1]]
↳ app/controllers/jobs_controller.rb:24
Impression Exists (0.4ms) SELECT 1 AS one FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2 AND "impressions"."session_hash" = $3 LIMIT $4 [["impressionable_id", 703], ["impressionable_type", "Job"], ["session_hash", "d80d52dd401011a626d600167140e49f"], ["LIMIT", 1]]
↳ app/controllers/jobs_controller.rb:24
I'm using the impressionist gem and using the impressionist method directly, taking all of the Jobs on the page and logging an impression. The problem is because I'm only recording unique impressions, for records that already have an impression, these additional calls are redundant. I tried to use #jobs.includes(:impressions).each to preload the associated data hoping Rails was smart enough to figure out which records existed, but Rails still outputs numerous Impression Exists queries.
#jobs.each{|job| impressionist(job,'', :unique => [:session_hash])}
I'm allowing a user to enter IP addresses in an input field which may be of different types delimited by a comma, such as (in no particular order):
192.168.1.1,192.168.2.1-25,10.10.10.0/24,192.168.1.2
This 'string' would get saved in my DB under device.ips_to_scan.
I want to validates_format_of on these, but am finding it a little difficult to write a regex that seems to work in rails, while it does work on regex101 (https://regex101.com/r/nf2bnM/1):
validates_format_of :ips_scan, with: /\A([0-9]{1,3}\.){3}[0-9]{1,3}(\/([1-2][0-9]|[0-9]|3[0-2]))?(-([0-9]{1,3}))?,?\Z/i, on: :update
This one is expected to fail:
Started PUT "/devices/2" for 127.0.0.1 at 2018-02-19 22:03:15 -0500
Processing by DevicesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"EQCFG6/xoJHtP6Nd3oqaYRW6mypfEoCMrnio1yj6loP+KtvjgLZ9Gmhb0oTwCjD0RGH+qQuctZFVIvF5HBJcGw==", "device"=>{"ips_scan"=>"192.168.1.1,192.168.2.1-25,a.b.c.d", "ips_exclude"=>"10.10.10.1"}, "commit"=>"Save", "id"=>"2"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Device Load (1.6ms) SELECT "devices".* FROM "devices" WHERE "devices"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
(0.5ms) BEGIN
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
(0.5ms) ROLLBACK
Redirected to http://localhost:3000/devices/2/edit
Completed 302 Found in 47ms (ActiveRecord: 12.1ms)
...But this one should have worked:
Processing by DevicesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"JJfmT/0l5MEDc+gUH/WHHp3bbgyzjGa0xTzaXM3E/WHLvbi30mI5SoYXmc0xdS2LzAALj+cCU6k+ZoPy+Sw3+Q==", "device"=>{"ips_scan"=>"192.168.1.1,192.168.2.1-25,192.168.1.2", "ips_exclude"=>"10.10.10.1"}, "commit"=>"Save", "id"=>"2"}
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Device Load (0.7ms) SELECT "devices".* FROM "devices" WHERE "devices"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
(0.6ms) BEGIN
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
(0.6ms) ROLLBACK
Redirected to http://localhost:3000/devices/2/edit
Completed 302 Found in 17ms (ActiveRecord: 3.5ms)
Last thing I can think of, is that I do have strong parameters, but I'm permitting ips_scan, so that this shouldn't be an issue:
def update
if #device.update(device_params)
flash[:notice] = 'Successful update'
respond_with :edit, :device
else
flash[:warning] = 'Unable to update'
respond_with :edit, :device
end
end
private def device_params
params.require(:device).permit(:token, :ips_scan, :ips_exclude)
end
I'm hoping you rubyist's out there have a eloquent solution. The first thought that comes to mind is that I have to split the string, and check each element sequentially to ensure it matches instead.
While I'm still open to a nice eloquent one-liner within the Model itself, I was able to get this working through creating a concern:
models/concerns/ip_validator.rb
class IpValidator < ActiveModel::Validator
def validate(record)
ips = record.ips_scan.split(',')
ips.each do |ip|
/([0-9]{1,3}\.){3}[0-9]{1,3}(\/([1-2][0-9]|[0-9]|3[0-2]))?(-([0-9]{1,3}))?/ =~ ip
record.errors.add(:ips_scan, ' is not valid') unless $LAST_MATCH_INFO
end
end
end
The call in my model now looks like:
validates :ips_scan, :ips_exclude, ip: true, on: :update
You can use this method in your custom validator to check an IP address
require 'ipaddr'
def valid_ip_addr?(ip_addr)
IPAddr.new(ip_addr)
true
rescue IPAddr::InvalidAddressError => _error
false
end
Does Rails actually cache the query result? The documentation says that same query will be never executed twice on the same request:
1.7 SQL Caching
The second time the same query is run against the database, it's not actually going to hit the database. The first time the result is returned from the query it is stored in the query cache (in memory) and the second time it's pulled from memory.
I did an experiment to proof that Rails actually cache the query:
def test
data = ""
User.find(1).update(first_name: 'Suwir Suwirr')
data << User.find(1).first_name
data << "\n"
User.find(1).update(first_name: 'Pengguna')
data << User.find(1).first_name
data << "\n"
render plain: data
end
If the result is cached, i would get the same result for each User.find(1). However, the result was Rails does not actually cache the query; i was expecting the update does not reflected on the result since it was "cached":
Suwir Suwirr
Pengguna
But the console says that it was cached: (Please highlight the CACHE word)
Started GET "/diag/test" for 10.0.2.2 at 2017-02-21 10:30:16 +0700
Processing by DiagController#test as HTML
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 4], ["LIMIT", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
SQL (0.4ms) UPDATE "users" SET "first_name" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["first_name", "Suwir Suwirr"], ["updated_at", 2017-02-21 03:30:16 UTC], ["id", 1]]
(16.5ms) COMMIT
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
SQL (0.3ms) UPDATE "users" SET "first_name" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["first_name", "Pengguna"], ["updated_at", 2017-02-21 03:30:16 UTC], ["id", 1]]
(0.9ms) COMMIT
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendering text template
Rendered text template (0.0ms)
Completed 200 OK in 380ms (Views: 3.5ms | ActiveRecord: 21.9ms)
So my question, does Rails actually cache the query result? Or, only several query result on some request?
Update: Using Batch #update_all
I made another experiment to "fool" the query logic. Now Rails does not "cache" the query. Why this behaviour can happen?
# Controller
def test
data = ""
User.where(id: 1).update_all(first_name: 'Suwir Suwirr')
data << User.find(1).first_name
data << "\n"
User.where(id: 1).update_all(first_name: 'Pengguna')
data << User.find(1).first_name
data << "\n"
logger.info 'hi'
render plain: data
end
# Console
Started GET "/diag/test" for 10.0.2.2 at 2017-02-21 10:45:43 +0700
Processing by DiagController#test as HTML
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 4], ["LIMIT", 1]]
SQL (13.8ms) UPDATE "users" SET "first_name" = 'Suwir Suwirr' WHERE "users"."id" = $1 [["id", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
SQL (2.9ms) UPDATE "users" SET "first_name" = 'Pengguna' WHERE "users"."id" = $1 [["id", 1]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
hi
Rendering text template
Rendered text template (0.0ms)
Completed 200 OK in 28ms (Views: 0.8ms | ActiveRecord: 17.8ms)
# Browser result
Suwir Suwirr
Pengguna
I was stupid.
Yes, Rails does actually cache the query, but update and destroy will invalidate its query cache. update_all is basically iterating each record with update.
I tried the experiment by really "fooling" the ActiveRecord query mechanism. And yes, it works.
# Controller
def test
data = ""
ActiveRecord::Base.connection.execute('UPDATE "users" SET "first_name" = \'Suwir Suwirr\' WHERE "users"."id" = 1')
data << User.find(1).first_name
data << "\n"
ActiveRecord::Base.connection.execute('UPDATE "users" SET "first_name" = \'Pengguna\' WHERE "users"."id" = 1')
data << User.find(1).first_name
data << "\n"
render plain: data
end
# Browser
Suwir Suwirr
Suwir Suwirr