Partially working simple image upload using Active Storage - ruby-on-rails

Any help or guidance would be most appreciated.
I've followed the Rails Guide, however, and I can upload an image to a model. However, it produces an error.
I've copied what the error suggests and that produces another error and does not upload an image.
Many, many thanks in advance!
This works but produces an error post form submission. If I hit back and then visit the specific prod id product#show the image has uploaded.
Error
NameError in ProductsController#create
undefined local variable or method `product' for # Did you mean? #product
Products.rb
def create
#list = List.find(params[:list_id])
#product = #list.products.create(product_params)
product.hero.attach(params[:hero])
redirect_to list_path(#list)
end
This does not work however, looks syntactically correct as the "#product" model attaches to the :hero .
Products.rb
def create
#list = List.find(params[:list_id])
#product = #list.products.create(product_params)
#product.hero.attach(params[:hero])
redirect_to list_path(#list)
end
creates the product
refreshes the screen back to the product list so it looks correct.
This breaks producing two errors:
The first error on products#show
ArgumentError in Products#show
Showing /Users/user/rubyonrails/shopping/app/views/products/show.html.erb where line #2 raised:
Can't resolve image into URL: to_model delegated to attachment, but the attachment is nil
Second terminal output presents the upload seems to work then something called Active Storage Purge fires up removing it?
ActiveStorage::Blob Load (0.3ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 7], ["LIMIT", 1]]
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] Performing ActiveStorage::PurgeJob (Job ID: e9b35a62-ff6c-4a38-8946-3aa9c19668ef) from Async(active_storage_purge) enqueued at 2020-04-15T11:31:41Z with arguments: #<GlobalID:0x00007f92b1da3d60 #uri=#<URI::GID gid://shopping/ActiveStorage::Blob/7>>
[ActiveJob] [ActiveStorage::AnalyzeJob] [b724849f-6993-4130-bef1-a0f8837a3171] (6.4ms) COMMIT
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] (0.8ms) BEGIN
[ActiveJob] [ActiveStorage::AnalyzeJob] [b724849f-6993-4130-bef1-a0f8837a3171] Performed ActiveStorage::AnalyzeJob (Job ID: b724849f-6993-4130-bef1-a0f8837a3171) from Async(active_storage_analysis) in 15.88ms
Started GET "/lists/9" for ::1 at 2020-04-15 12:31:41 +0100
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] ActiveStorage::Attachment Exists? (0.5ms) SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."blob_id" = $1 LIMIT $2 [["blob_id", 7], ["LIMIT", 1]]
Processing by ListsController#show as HTML
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] ActiveStorage::Attachment Load (1.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 7], ["record_type", "ActiveStorage::Blob"], ["name", "preview_image"], ["LIMIT", 1]]
Parameters: {"id"=>"9"}
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] ActiveStorage::Blob Destroy (1.0ms) DELETE FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 [["id", 7]]
List Load (0.9ms) SELECT "lists".* FROM "lists" WHERE "lists"."id" = $1 LIMIT $2 [["id", 9], ["LIMIT", 1]]
↳ app/controllers/lists_controller.rb:7:in `show'
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 9], ["LIMIT", 1]]
↳ app/controllers/lists_controller.rb:8:in `show'
Rendering lists/show.html.erb within layouts/application
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/views/lists/show.html.erb:2
Product Exists? (0.2ms) SELECT 1 AS one FROM "products" WHERE "products"."list_id" = $1 LIMIT $2 [["list_id", 9], ["LIMIT", 1]]
↳ app/views/lists/show.html.erb:5
Product Load (0.2ms) SELECT "products".* FROM "products" WHERE "products"."list_id" = $1 [["list_id", 9]]
↳ app/views/lists/show.html.erb:6
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] (6.4ms) COMMIT
(0.8ms) SELECT COUNT(*) FROM "products" WHERE "products"."list_id" = $1 [["list_id", 9]]
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] Disk Storage (0.2ms) Deleted file from key: 31cfuow9pj6vjqhq8i479fdxf1lc
↳ app/controllers/application_controller.rb:5:in `product_list_size?'
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] Disk Storage (0.1ms) Deleted files by key prefix: variants/31cfuow9pj6vjqhq8i479fdxf1lc/
Rendered lists/show.html.erb within layouts/application (Duration: 9.5ms | Allocations: 7278)
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] Performed ActiveStorage::PurgeJob (Job ID: e9b35a62-ff6c-4a38-8946-3aa9c19668ef) from Async(active_storage_purge) in 22.5ms
[Webpacker] Everything's up-to-date. Nothing to do
List item

Egg on my face.
The exclamation mark omission seems to be the issue.
from
#product = #list.products.create(product_params)
to this and it works
#product = #list.products.create!(product_params)

Related

Solidus Paypal checkout fails in sandbox for non-PayPal payment variants

I have built a webshop in Ruby on Rails using Solidus.
When I try to checkout in the PayPal sandbox with credit card or SEPA with a German address, it complains that the shop does not deliver there.
It works fine when I use PayPal payment with an account, also to a German address in Berlin...
This is the error in the ruby console:
Started GET "/solidus_paypal_commerce_platform/shipping_rates?order_id=R977809399&order_token=[FILTERED]&address%5Bcity%5D=Berlin&address%5Bstate%5D=&address%5Bcountry_code%5D=DE&address%5Bpostal_code%5D=10117" for 127.0.0.1 at 2022-09-29 20:29:49 +0200
Processing by SolidusPaypalCommercePlatform::ShippingRatesController#simulate_shipping_rates as */*
Parameters: {"order_id"=>"R977809399", "order_token"=>"[FILTERED]", "address"=>{"city"=>"Berlin", "state"=>"", "country_code"=>"DE", "postal_code"=>"10117"}}
Spree::User Load (0.1ms) SELECT "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."spree_api_key" = ? LIMIT ? [["spree_api_key", "undefined"], ["LIMIT", 1]]
Spree::Order Load (0.1ms) SELECT "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."number" = ? LIMIT ? [["number", "R977809399"], ["LIMIT", 1]]
Spree::User Load (0.1ms) SELECT "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
CACHE Spree::Order Load (0.0ms) SELECT "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."number" = ? LIMIT ? [["number", "R977809399"], ["LIMIT", 1]]
CACHE Spree::User Load (0.0ms) SELECT "spree_users".* FROM "spree_users" WHERE "spree_users"."deleted_at" IS NULL AND "spree_users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
TRANSACTION (0.0ms) begin transaction
Spree::Country Load (0.1ms) SELECT "spree_countries".* FROM "spree_countries" WHERE "spree_countries"."iso" = ? LIMIT ? [["iso", "DE"], ["LIMIT", 1]]
Spree::State Load (0.1ms) SELECT "spree_states".* FROM "spree_states" WHERE "spree_states"."country_id" = ? AND "spree_states"."abbr" = ? ORDER BY "spree_states"."name" ASC LIMIT ? [["country_id", 57], ["abbr", ""], ["LIMIT", 1]]
Spree::State Load (0.1ms) SELECT "spree_states".* FROM "spree_states" WHERE "spree_states"."country_id" = ? AND "spree_states"."name" = ? ORDER BY "spree_states"."name" ASC LIMIT ? [["country_id", 57], ["name", ""], ["LIMIT", 1]]
Spree::Store Load (0.0ms) SELECT "spree_stores".* FROM "spree_stores" WHERE "spree_stores"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Spree::Order Exists? (0.1ms) SELECT 1 AS one FROM "spree_orders" WHERE "spree_orders"."number" = ? AND "spree_orders"."id" != ? LIMIT ? [["number", "R977809399"], ["id", 10], ["LIMIT", 1]]
Spree::Adjustment Load (0.0ms) SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."order_id" = ? AND "spree_adjustments"."source_type" = ? [["order_id", 10], ["source_type", "Spree::PromotionAction"]]
Spree::LineItem Load (0.0ms) SELECT "spree_line_items".* FROM "spree_line_items" WHERE "spree_line_items"."order_id" = ? ORDER BY "spree_line_items"."created_at" ASC, "spree_line_items"."id" ASC [["order_id", 10]]
Spree::Product Load (0.0ms) SELECT "spree_products".* FROM "spree_products" INNER JOIN "spree_variants" ON "spree_products"."id" = "spree_variants"."product_id" WHERE "spree_variants"."id" = ? LIMIT ? [["id", 45], ["LIMIT", 1]]
TRANSACTION (0.0ms) rollback transaction
Completed 422 Unprocessable Entity in 35ms (Views: 0.2ms | ActiveRecord: 0.8ms | Allocations: 31326)
Would be grateful for any pointers where to adjust that,
as I cannot make head or tail of it.
I don't know why Solidus is erroring internally, but I can tell you that if the rendered PayPal button has an onShippingChange callback yet that callback does not respond with success or times out (due to some error, generally), the PayPal checkout will give that message since you're indicating the address needs to be checked during payment approval yet are not responding that it's OK when queried.
Another possible flow design -- likely not used/offered by Solidus -- is to not specify onShippingChange and to check the address after approval (but before final order capture) and provide a mechanism to patch it when it's not a valid address you'll accept.

Rails: ActiveRecord perform pointless query multiple times over the same model

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.

How to use devise_invitable properly with activeadmin ? (issues to send emails)

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

Rails 5 : Devise Oauth Referral param not saved

I have an app where user can register using email, facebook or google, also I have small referral system where user can share link and earn points.
Now everything works perfectly registering and login. But referral system works only when creating account using social networks.
the following is my code :
omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, event: :authentication
#this will throw if #user is not activated
set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
# google callback
def google_oauth2
#user = User.from_omniauth(request.env['omniauth.auth'])
if #user.persisted?
sign_in_and_redirect #user
set_flash_message(:notice, :success, kind: 'Google') if is_navigational_format?
else
flash[:error] = 'There was a problem signing you in through Google. Please register or try signing in later.'
redirect_to new_user_registration_url
end
end
def failure
flash[:error] = 'There was a problem signing you in. Please register or try signing in later.'
redirect_to new_user_registration_url
end
end
registrations_controller.rb
def build_resource(hash = {})
super
if cookies[:referral_code] && referrer = User.find_by(referral_code: cookies[:referral_code])
self.resource.referred_by = referrer
end
end
user.rb
before_validation :set_referral_code
validates :referral_code, uniqueness: true
def set_referral_code
loop do
self.referral_code = SecureRandom.hex(6)
break unless self.class.exists?(referral_code: referral_code)
end
end
application_controller.rb
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :set_referral_cookie
def set_referral_cookie
if params[:ref]
cookies[:referral_code] = {
value: params[:ref],
expires: 30.days.from_now,
}
end
end
I built my system using help of this repository
gorails referral system
Edit
Started GET "/?ref=6c28f9668715" for 127.0.0.1 at 2019-01-27 21:23:32 +0100
Processing by HomeController#index as HTML
Parameters: {"ref"=>"6c28f9668715"}
Rendering home/index.html.erb within layouts/application
Rendered home/index.html.erb within layouts/application (0.6ms)
Rendered shared/_navbar.html.erb (1.4ms)
Completed 200 OK in 125ms (Views: 90.7ms | ActiveRecord: 0.0ms)
Started GET "/users/sign_up" for 127.0.0.1 at 2019-01-27 21:23:34 +0100
Processing by Users::RegistrationsController#new as HTML
User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."referral_code" = $1 LIMIT $2 [["referral_code", "6c28f9668715"], ["LIMIT", 1]]
↳ app/controllers/users/registrations_controller.rb:7
Rendering users/registrations/new.html.erb within layouts/application
Rendered users/registrations/new.html.erb within layouts/application (6.7ms)
Rendered shared/_navbar.html.erb (1.2ms)
Completed 200 OK in 300ms (Views: 142.0ms | ActiveRecord: 1.6ms)
Started GET "/users/auth/facebook" for 127.0.0.1 at 2019-01-27 21:23:37 +0100
I, [2019-01-27T21:23:37.266108 #67828] INFO -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook" for 127.0.0.1 at 2019-01-27 21:23:38 +0100
I, [2019-01-27T21:23:38.488342 #67828] INFO -- omniauth: (facebook) Request phase initiated.
Started GET "/users/auth/facebook/callback?code=AQAemLam_HRACk1q5NTfix5Sve6rA1fsDD5z_21vKpDlaTq4hfuXM2Oh_CThPspwk1BIg4Tjc1bm0UOcXLo_X0XGVI8XdLsirhPV6wKnGiCO3uU3l4y6y31qnhC1xjzd-21wx_cWVO-ipPCzrZ8kqWCdvQrxxKOQXMj10LsKlTAbuSqMEpx90XvcZw3RAYGLSiEFQGJSgCpABpboh_n_ewjTbfbTB01JATW6hM9Wy8iN1AQLpXrRgOZ-5P1NdowqdHdjU420N6QoB7R9tyHXegioQ47J8cjgCMUFwDPi_T--zHK6_-sIkW_QE6P5ryot1qHxzHpOASvx46WHvJun5_Yh&state=bd3dfbf7e7a5c83c42d1d754149d7be5ea7f39b6d5b99b28" for 127.0.0.1 at 2019-01-27 21:23:39 +0100
I, [2019-01-27T21:23:39.483990 #67828] INFO -- omniauth: (facebook) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#facebook as HTML
Parameters: {"code"=>"AQAemLam_HRACk1q5NTfix5Sve6rA1fsDD5z_21vKpDlaTq4hfuXM2Oh_CThPspwk1BIg4Tjc1bm0UOcXLo_X0XGVI8XdLsirhPV6wKnGiCO3uU3l4y6y31qnhC1xjzd-21wx_cWVO-ipPCzrZ8kqWCdvQrxxKOQXMj10LsKlTAbuSqMEpx90XvcZw3RAYGLSiEFQGJSgCpABpboh_n_ewjTbfbTB01JATW6hM9Wy8iN1AQLpXrRgOZ-5P1NdowqdHdjU420N6QoB7R9tyHXegioQ47J8cjgCMUFwDPi_T--zHK6_-sIkW_QE6P5ryot1qHxzHpOASvx46WHvJun5_Yh", "state"=>"bd3dfbf7e7a5c83c42d1d754149d7be5ea7f39b6d5b99b28"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT $3 [["provider", "facebook"], ["uid", "311741132783400"], ["LIMIT", 1]]
↳ app/models/user.rb:60
Disk Storage (5.7ms) Uploaded file to key: Sp8DiDxRWFn3uUfGTJJHdABc (checksum: tjmoDYRADOqNyXiWy90gxg==)
(0.3ms) BEGIN
↳ app/models/user.rb:66
ActiveStorage::Blob Create (44.9ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "byte_size", "checksum", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["key", "Sp8DiDxRWFn3uUfGTJJHdABc"], ["filename", "avatar.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["byte_size", 1640], ["checksum", "tjmoDYRADOqNyXiWy90gxg=="], ["created_at", "2019-01-27 20:23:42.423862"]]
↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:12
(0.9ms) COMMIT
↳ app/models/user.rb:66
(0.2ms) BEGIN
↳ app/models/user.rb:66
(0.2ms) COMMIT
↳ app/models/user.rb:66
(0.2ms) BEGIN
↳ app/models/user.rb:60
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 AND "users"."referral_code" = $3 LIMIT $4 [["provider", "facebook"], ["uid", "311741132783400"], ["referral_code", "2925f372e912"], ["LIMIT", 1]]
↳ app/models/user.rb:79
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "qubitam#gmail.com"], ["LIMIT", 1]]
↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/rails5_1/uniqueness_validator.rb:38
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."referral_code" = $1 LIMIT $2 [["referral_code", "2925f372e912"], ["LIMIT", 1]]
↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/rails5_1/uniqueness_validator.rb:38
User Create (2.5ms) INSERT INTO "users" ("email", "encrypted_password", "name", "created_at", "updated_at", "provider", "uid", "referral_code") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["email", "qubitam#gmail.com"], ["encrypted_password", "$2a$11$JUxrDl1I4uDQwxi3yRVrnuqRJNYQJA8BcvLrmAj29nwBggJknrP3i"], ["name", "Abdelmoumin Mokhtari"], ["created_at", "2019-01-27 20:23:42.483417"], ["updated_at", "2019-01-27 20:23:42.483417"], ["provider", "facebook"], ["uid", "311741132783400"], ["referral_code", "2925f372e912"]]
↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:12
ActiveStorage::Attachment Create (0.7ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "image"], ["record_type", "User"], ["record_id", 13], ["blob_id", 10], ["created_at", "2019-01-27 20:23:42.487979"]]
↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:12
User Update (41.5ms) UPDATE "users" SET "updated_at" = $1 WHERE "users"."id" = $2 [["updated_at", "2019-01-27 20:23:42.490719"], ["id", 13]]
↳ app/models/user.rb:60
(40.9ms) COMMIT
↳ app/models/user.rb:60
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 1f7ce124-dc8b-4302-912e-7b1138656e35) to Async(default) with arguments: #<GlobalID:0x00007fa636abf0e8 #uri=#<URI::GID gid://classifyads/ActiveStorage::Blob/10>>
(1.0ms) BEGIN
↳ app/controllers/users/omniauth_callbacks_controller.rb:8
ActiveStorage::Blob Load (0.4ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 10], ["LIMIT", 1]]
↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] Performing ActiveStorage::AnalyzeJob (Job ID: 1f7ce124-dc8b-4302-912e-7b1138656e35) from Async(default) with arguments: #<GlobalID:0x00007fa636aac790 #uri=#<URI::GID gid://classifyads/ActiveStorage::Blob/10>>
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] Disk Storage (0.2ms) Downloaded file from key: Sp8DiDxRWFn3uUfGTJJHdABc
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] Skipping image analysis because the mini_magick gem isn't installed
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] (0.2ms) BEGIN
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] ActiveStorage::Blob Update (0.6ms) UPDATE "active_storage_blobs" SET "metadata" = $1 WHERE "active_storage_blobs"."id" = $2 [["metadata", "{\"identified\":true,\"analyzed\":true}"], ["id", 10]]
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:7
User Update (82.8ms) UPDATE "users" SET "sign_in_count" = $1, "current_sign_in_at" = $2, "last_sign_in_at" = $3, "current_sign_in_ip" = $4, "last_sign_in_ip" = $5, "updated_at" = $6 WHERE "users"."id" = $7 [["sign_in_count", 1], ["current_sign_in_at", "2019-01-27 20:23:42.581519"], ["last_sign_in_at", "2019-01-27 20:23:42.581519"], ["current_sign_in_ip", "127.0.0.1/32"], ["last_sign_in_ip", "127.0.0.1/32"], ["updated_at", "2019-01-27 20:23:42.584122"], ["id", 13]]
↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/bundler/gems/globalize-3fe2f93ab2d0/lib/patches/active_record/persistence.rb:7
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] (61.8ms) COMMIT
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] ↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
[ActiveJob] [ActiveStorage::AnalyzeJob] [1f7ce124-dc8b-4302-912e-7b1138656e35] Performed ActiveStorage::AnalyzeJob (Job ID: 1f7ce124-dc8b-4302-912e-7b1138656e35) from Async(default) in 122.16ms
(79.1ms) COMMIT
↳ app/controllers/users/omniauth_callbacks_controller.rb:8
Redirected to http://localhost:3000/users/edit
Completed 302 Found in 2383ms (ActiveRecord: 297.1ms)
Started GET "/users/edit" for 127.0.0.1 at 2019-01-27 21:23:42 +0100
Processing by Users::RegistrationsController#edit as HTML
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 13], ["LIMIT", 1]]
↳ /Users/qubitam/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Rendering users/registrations/edit.html.erb within layouts/application
ActiveStorage::Attachment Load (0.6ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 13], ["record_type", "User"], ["name", "image"], ["LIMIT", 1]]
↳ app/views/users/registrations/edit.html.erb:8
ActiveStorage::Blob Load (0.9ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 10], ["LIMIT", 1]]
↳ app/views/users/registrations/edit.html.erb:8
Rendered users/registrations/edit.html.erb within layouts/application (14.4ms)
Rendered shared/_navbar.html.erb (3.9ms)
Completed 200 OK in 105ms (Views: 100.1ms | ActiveRecord: 2.2ms)

Rails - Increasing performance of repeated if statement

I'm using the public_activity gem and in the output, I'm checking if the trackable owner is the same as the current user:
= a.owner == current_user ? 'You' : a.owner.name
did this activity
I get a bunch of cache calls in the log:
User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendered public_activity/post/_create.html.haml (1.4ms)
Rendered public_activity/_snippet.html.haml (11.4ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendered public_activity/post/_create.html.haml (13.9ms)
Rendered public_activity/_snippet.html.haml (18.9ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendered public_activity/comment/_comment.html.haml (0.9ms)
Rendered public_activity/_snippet.html.haml (12.1ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendered public_activity/comment/_comment.html.haml (2.7ms)
Rendered public_activity/_snippet.html.haml (56.3ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendered public_activity/comment/_comment.html.haml (0.6ms)
Rendered public_activity/_snippet.html.haml (4.5ms)
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Rendered public_activity/content/_comment.html.haml (2.1ms)
Rendered public_activity/_snippet.html.haml (9.5ms)
Is there any way to eager load the conditional?
#jverban is correct that you can compare the record IDs to avoid needless record loading. To answer your question about eager loading though, yes you can eager load using the includes method in the ActiveRecord query chain. For example:
Activity.includes(:owner).latest
That will tell Rails you intend to reference the owner relation and so they should be loaded as well.
I highly recommend adding the bullet gem to your project (only in development and test environments) to detect N+1 queries and warn you when you've got an N+1 query situation like this happening.
You shouldn't need to load the user record, just compare id attributes
= a.owner_id == current_user.id ? 'You' : a.owner.name
The cache calls will likely still happen if multiple activity owners are not the current user (to get the owner name).

Resources