Ruby on Rails Random Notes

Use FactoryBot from development rails console

include FactoryBot::Syntax::Methods
include ActionDispatch::TestProcess

Redis not running or wrong host

Errno::EADDRNOTAVAIL (Cannot assign requested address - connect(2) for [::1]:6379

Redis isn't running on localhost. Set environment variable REDIS_URL to real address like redis://redis:6379/0.

Url helper requires a URL

ActionView::Template::Error (Nil location provided. Can't build URI.)URL cannot be nil for some URL helpers in Rails.

Solution 1: Use default cover image:

image_tag(@article.cover.url || default_cover_url)

Solution 2: Don't show any cover image if it's not there

<% if @article.cover %>
<% end %>

Webpacker In Rails Engine Doesn't Work

Webpacker in Rails 6.0.3 engines is not integrated very well. I mean spend two weeks configuring webpack and it might work. And will be coupled. I tried to follow three guides, but two of them had serious limitations and one didn't work.


  1) welcome page shows articles
     Failure/Error: <%= stylesheet_pack_tag 'application', media: 'all' %>

       undefined method `stylesheet_pack_tag' for #<#<Class:0x00007f9678134ca8>:0x00007f967813f3b0>
       Did you mean?  stylesheet_path
     # ./spec/dummy/app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb__3341715273681785289_70142118227440'
     # ./spec/requests/welcome_spec.rb:6:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `stylesheet_pack_tag' for #<#<Class:0x00007f9678134ca8>:0x00007f967813f3b0>
     #   Did you mean?  stylesheet_path
     #                  stylesheet_link_tag
     #   ./spec/dummy/app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb__3341715273681785289_70142118227440'

Finished in 0.50944 seconds (files took 3.19 seconds to load)
10 examples, 1 failure

How to Run Sidekiq

Command to run Sidekiq from application directory.

bundle exec sidekiq

Inspecting Network Traffic In Capybara Driver

I think this was Apparition, Chrome Headless. Useful command because sometimes weird shit happens.

puts{|traffic| [traffic.url, traffic.method, traffic.response&.status, traffic.error&.code, traffic.error&.description]}.inspect

Inspecting Sidekiq Queues Directly With Redis Commands

Because sometimes Sidekiq interface is not available. Commands are specific for types, therefore the most important thing is to list data and it's type. Using from Redis CLI will be similar.

# rails console
r =

# list keys
pp{|k| k !~ /stat/ }.map{|k| [k, r.type(k)]}; nil
[["queue:api", "list"],
 ["dead", "zset"],
 ["queues", "set"],
 ["retry", "zset"],
 ["ec5ec2a0e9d3:1:fe1000f40d66", "hash"],
 ["processes", "set"],
 ["schedule", "zset"]]
=> nil

r.hgetall "ec5ec2a0e9d3:1:fe1000f40d66"
=> {"quiet"=>"false", "info"=>"{\"hostname\":\"ec5ec2a0e9d3\",\"started_at\":1579787753.5046794,\"pid\":1,\"tag\":\"app\",\"concurrency\":25,\"queues\":[\"default\",\"mailers\"],\"labels\":[],\"identity\":\"ec5ec2a0e9d3:1:fe1000f40d66\"}", "beat"=>"1579791952.047832", "busy"=>"0"}

r.lrange "queue:api", 0, 100
=> ["{\"class\":\"ApiWorker\",\"args\":[\"26\"],\"retry\":true,\"queue\":\"api\",\"throttle\":{\"threshold\":4,\"period\":\"15\"},\"backtrace\":true,\"jid\":\"e7b7ca29e568cf1a1fd2570a\",\"created_at\":1562264868.547173,\"enqueued_at\":1562264879.8490522}", "{\"class\":\"ApiWorker\",\"args\":[\"30\"],\"retry\":true,\"queue\":\"api\",\"throttle

r.smembers "processes"
=> ["ec5ec2a0e9d3:1:fe1000f40d66"]

r.smembers "queues"
=> ["default", "mailers", "api"]

LOADING Redis is loading the dataset in memory (Redis::CommandError)

/usr/local/bundle/gems/redis-4.1.4/lib/redis/client.rb:126:in `call': LOADING Redis is loading the dataset in memory (Redis::CommandError)

This really means Redis is still loading data. If Redis is running in --append-only mode, database file (appendonly.aof) will only get larger until it has couple of gigabytes or more and takes couple of minutes to load.

Needs to be compacted regularly, to get rid of old data.

Background append only file rewriting started

It will compact couple of gigabytes into couple of megabytes. For Rails app.