I'm working on an app that I didn't develop, so there are many things about the architecture that are not clear, yet. My goal is just to get it up and going at this point. I'm helping an organization upgrade a Rails built on 4.2 to run on 6.0.2. I've gone to Railsdiff and other places to learn to change.
After updating and replacing gems, and making other Rails 6 changes, I was able to get the application to boot. The problem that I'm running into is a 500 Internal Server Error and it refers to ActiveRecord. The log shows that the app is arriving at the correct controller/action. It shows where I dumped a couple of records to make sure it was following the app path. It fails when it tries to render the layout and views. Examining the page shows that nothing is rendered. No HTML elements or meta data about the page is visible.
Here is a dump from my development.log file. Everything looks good until the end. If you have experienced this in the past and have suggestions, I would appreciate any suggestions.
Zeitwerk#rails.main: constant CoursesController loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/controllers/courses_controller.rb
Zeitwerk#rails.main: constant CorporationsController loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/controllers/corporations_controller.rb
Processing by CorporationsController#show as HTML
Zeitwerk#rails.main: constant Corporation loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/models/corporation.rb
Corporation Load (1.5ms) SELECT "corporations".* FROM "corporations" WHERE "corporations"."subdomain" = $1 LIMIT $2 [["subdomain", "devdomain"], ["LIMIT", 1]]
Zeitwerk#rails.main: constant DeviseController loaded from file /usr/local/rvm/gems/ruby-2.5.1#academy/gems/devise-4.7.1/app/controllers/devise_controller.rb
Zeitwerk#rails.main: constant ImageUploader loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/uploaders/image_uploader.rb
Zeitwerk#rails.main: constant Announcement loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/models/announcement.rb
Corporation Load (1.7ms) SELECT "corporations".* FROM "corporations" WHERE "corporations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Zeitwerk#rails.main: constant PaperTrail::Version loaded from file /usr/local/rvm/gems/ruby-2.5.1#academy/gems/paper_trail-10.3.1/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb
Zeitwerk#rails.main: constant VideoUploader loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/uploaders/video_uploader.rb
Zeitwerk#rails.main: constant Course loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/models/course.rb
Course Load (2.5ms) SELECT "courses".* FROM "courses" WHERE "courses"."deleted_at" IS NULL AND "courses"."corporation_id" = $1 ORDER BY lower(TITLE) ASC [["corporation_id", 1]]
CourseSchedule Load (1.5ms) SELECT "course_schedules".* FROM "course_schedules" INNER JOIN "courses" ON "courses"."id" = "course_schedules"."course_id" WHERE "course_schedules"."deleted_at" IS NULL AND "courses"."corporation_id" = $1 AND 1=0 [["corporation_id", 1]]
Zeitwerk#rails.main: constant BackgroundUploader loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/uploaders/background_uploader.rb
Zeitwerk#rails.main: constant Instsplash loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/models/instsplash.rb
Instsplash Load (1.2ms) SELECT "instsplashes".* FROM "instsplashes" WHERE "instsplashes"."corporation_id" = $1 AND "instsplashes"."landing" = $2 ORDER BY "instsplashes"."id" ASC LIMIT $3 [["corporation_id", 1], ["landing", true], ["LIMIT", 1]]
Redirected to http://dev-account-on-ec.compute.amazonaws.com:3005/instsplashes/1
Completed 302 Found in 305ms (ActiveRecord: 50.0ms | Allocations: 139299)
Started GET "/instsplashes/1" for location at 2020-03-23 16:27:19 +0000
Zeitwerk#rails.main: constant InstsplashesController loaded from file /home/devuser/webapps/dev/mydevdir/academy/app/controllers/instsplashes_controller.rb
Processing by InstsplashesController#show as HTML
Parameters: {"id"=>"1"}
Corporation Load (1.2ms) SELECT "corporations".* FROM "corporations" WHERE "corporations"."subdomain" = $1 LIMIT $2 [["subdomain", "devdomain"], ["LIMIT", 1]]
Instsplash Load (1.2ms) SELECT "instsplashes".* FROM "instsplashes" WHERE "instsplashes"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Corporation Load (1.2ms) SELECT "corporations".* FROM "corporations" WHERE "corporations"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
Course Load (1.3ms) SELECT "courses".* FROM "courses" WHERE "courses"."deleted_at" IS NULL AND "courses"."corporation_id" = $1 ORDER BY "courses"."title" ASC LIMIT $2 [["corporation_id", 1], ["LIMIT", 1]]
####COPORATION FOUND IN INSTSPLASHES SHOW {"id"=>1, "approved"=>nil, "request_id"=>nil, "created_at"=>Fri, 20 Mar 2020 14:06:00 UTC +00:00, "updated_at"=>Mon, 23 Mar 2020 16:24:27 UTC +00:00, "name"=>"Dev Account", "stripe_id"=>nil, "facebook"=>nil, "twitter"=>nil, "linkedin"=>nil, "google"=>nil, "support_email"=>nil, "pinterest"=>nil, "you_tube"=>nil, "instagram"=>nil, "tumblr"=>nil, "support_url"=>nil, "phone"=>nil, "badges_on"=>false, "preset_badges_on"=>true, "course_expiration"=>nil, "rev_share"=>100, "no_tax"=>nil, "discussion_on"=>nil, "rating_displayed"=>nil, "feedback_displayed"=>nil, "training_cycle_start"=>nil, "training_cycle_end"=>nil, "website"=>nil, "use_training_cycle"=>nil, "show_blog"=>nil, "allows_sign_up"=>nil, "about"=>nil, "course_approval"=>nil, "enrollment_approval"=>nil, "show_generic_troubleshoots"=>true, "po_requires_approval"=>true, "hide_cart"=>true, "saml_metadata"=>nil, "numanagesettings"=>{}, "saml_privatekey"=>nil, "saml_certificate"=>nil, "payment_gateway"=>nil, "payment_key"=>nil, "payment_secret"=>nil, "service_code"=>nil, "local_ref_pay"=>nil, "allow_instructor_ratings"=>nil, "qr_code"=>nil, "selected_home"=>"corporation", "saml_privatekey_kms_enc"=>nil, "saml_certificate_kms_enc"=>nil, "saml_metadata_kms_enc"=>nil, "course_per_cycle"=>1, "live_classroom_on"=>false, "validate_instructor_sme"=>nil, "show_course_enrollments"=>true, "show_course_modules"=>true, "twilio_service_on"=>false, "twillio_from_number"=>nil, "twillio_account_sid"=>nil, "twillio_auth_token"=>nil, "volunteer_toggle"=>false, "need_organization"=>false}
----INSTSPLASH FOUND IN INSTSPLASHES SHOW {"id"=>1, "corporation_id"=>1, "heading"=>"Page Heading", "name"=>"Page Name", "organizations_on"=>true, "show_courses"=>true, "text_color"=>"#ffffff", "button_color"=>"#543939", "background_color"=>"#ebebeb", "overlay_color"=>"#522b2b", "overlay_opacity"=>"0.45", "landing"=>true, "background_images"=>nil, "heading_size"=>20}
Rendering instsplashes/show.html.erb within layouts/splashpage
Rendered instsplashes/show.html.erb within layouts/splashpage (Duration: 84.5ms | Allocations: 78655)
Completed 500 Internal Server Error in 104ms (ActiveRecord: 4.9ms | Allocations: 83856)
I discovered my problem. It is in the asset pipeline. For some reason it is not finding the assets. I discovered this by commenting out the stylesheet_link_tag's, javascript_link_tag's, and image_tag's. The app started working. Also, there were several UI gems that I had to require in the application.rb file. I'm going to close this out since I know where the problem lies.
Related
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.
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)
I am developing an API in Rails and interacting with it from another Rails application. Up to now everything has been working correctly, using POST and GET to authenticate users, create records and retrieve data. But now I'm having trouble getting the PUT operation to update a record. It appears it is calling the API correctly and the API is returning a 200 response, but looking at the logs it doesn't look like Rails is even executing an UPDATE statement. The strangest thing is that there are no errors or warning.
Here is the Log. (Data is faker data so I'm not bothering to redact it)
Started PUT "/affiliates/17" for ::1 at 2019-04-26 17:42:51 -0400
Processing by AffiliatesController#update as HTML
Parameters: {"id"=>"17", "username"=>"teracole", "email"=>"dan#purdy.io", "first_name"=>"Max", "last_name"=>"Max", "company"=>"Dach LLC", "manager_id"=>"3"}
[1m[36mUser Load (0.3ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2[0m [["id", 2], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:12
[1m[36mAffiliate Load (0.2ms)[0m [1m[34mSELECT "affiliates".* FROM "affiliates" WHERE "affiliates"."id" = $1 LIMIT $2[0m [["id", 17], ["LIMIT", 1]]
↳ app/controllers/affiliates_controller.rb:50
[1m[35m (0.1ms)[0m [1m[35mBEGIN[0m
↳ app/controllers/affiliates_controller.rb:31
[1m[36mAffiliate Exists (0.3ms)[0m [1m[34mSELECT 1 AS one FROM "affiliates" WHERE "affiliates"."username" = $1 AND "affiliates"."id" != $2 LIMIT $3[0m [["username", "tera.cole"], ["id", 17], ["LIMIT", 1]]
↳ app/controllers/affiliates_controller.rb:31
[1m[35m (0.1ms)[0m [1m[35mCOMMIT[0m
↳ app/controllers/affiliates_controller.rb:31
Completed 200 OK in 8ms (Views: 0.9ms | ActiveRecord: 1.0ms)
You can see the parameter data in the log. The controller action is pretty simple:
# PATCH/PUT /affiliates/1
def update
if #affiliate.update(affiliate_params)
render json: #affiliate
else
render json: #affiliate.errors, status: :unprocessable_entity
end
end
Thanks in advance for any help.
As it turns out, for whatever reason, the put method required me to identify the content type in the header. Post wasn't doing this, but changing the line in my HttParty call fixed the issue:
response = HTTParty.put(put_uri, body: #this_data.to_json, headers: { 'Content-Type' => 'application/json', 'Authorization' => #token })
I am going though Michael Hartl's rails Tutorial chapter 10 section 10.2. And my rails app is now showing a blank page for all pages I try to load on the local host. I have tried resetting the computer as suggested in another post, but this didn't work. I have checked the development log and here is the error message.
Started GET "/users/3" for 95.10.6.124 at 2017-11-05 16:36:43 +0000
Cannot render console from 95.10.6.124! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#show as HTML
Parameters: {"id"=>"3"}
[1m[36mUser Load (0.2ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 3], ["LIMIT", 1]]
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (1.7ms)
Rendered layouts/_rails_default.html.erb (27.1ms)
Rendered layouts/_shim.html.erb (0.5ms)
[1m[36mUser Load (0.2ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m [["id", 3], ["LIMIT", 1]]
Rendered layouts/_header.html.erb (2.3ms)
Rendered layouts/_footer.html.erb (0.5ms)
Completed 200 OK in 41ms (Views: 38.0ms | ActiveRecord: 0.4ms)
This is strange as I undid the changes I made and now it doesnt work. In fact the serve doesnt seem to start. Instead of seeing the usual "ctrl+c to stop" at the end, the command prompt appears. This hasnt happened before and to my mnowledge I havent done anything differently.
Could someone help me with this, thanks.
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).