Skip to content

Instantly share code, notes, and snippets.

@TRex22
Last active March 18, 2024 14:08
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TRex22/00cf239c14be94134d9fc774127f1b78 to your computer and use it in GitHub Desktop.
Save TRex22/00cf239c14be94134d9fc774127f1b78 to your computer and use it in GitHub Desktop.
Useful Ruby/Rails console tricks

Get all records in state

Rails.application.eager_load!
ActiveRecord::Base.subclasses(&:name)

Get last console output

variable = _

Run a rails console in Sandbox mode

rails console --sandbox

Reload state

reload! model.reload

Get source of a method/model

.source_location

Get source code of a method

.source.display

Get Rails secrets

Rails.application.secrets.

binding.pry specific

next
step
continue
quit
whereami

exit-program # if you are stuck in a loop
!!! # if you are stuck in a loop
exit-p
exit! # will kill everything including the server

disable-pry
ENV['DISABLE_PRY'] = nil # to re-enable

Bash-a-like commands which can be run on models or in states

cd # into a model
ls # inside a model to list what it has
grep # a list of data?

Slim useful stuff (also if you have the pry gem)

= debug @variable
- binding.pry

Save console output to file (irb, rails console, pry)

f = File.new("file.type", 'w') # will be saved in the path that the console was started or you can define your own path
f << method.output; # ; means it wont spit out to your console
f.close # to save

Rails

Route Helper

def route_helper
  Rails.application.routes.url_helpers
end

Bundle

Update bundler

gem install bundler

Useful Bundle Commands

bundle outdated
bundle outdated --pre # pre-release gems
bundle install --path path to install gems
bundle install --system # removes path setting
bundle clean
bundle install --standalone # for a standalone bundle i.e. to copy to a server

If dtrace is not working correctly when trying to build packages (like gems or npm packages)

rvm install 2.3.8 -- --disable-dtrace

When using a clone of openssl

rvm reinstall 2.3.8 --with-openssl-dir=/usr/local/opt/openssl

Set Default Ruby Version Using RVM

rvm --default use 2.3.8

Other Issues

https://github.com/guard/guard/wiki/Add-Readline-support-to-Ruby-on-Mac-OS-X

If rails generate hangs

Might be caused by generated binstubs (https://github.com/rbenv/rbenv/wiki/Understanding-binstubs) See: https://github.com/sstephenson/rbenv/wiki/Understanding-binstubs

To reset the binstubs, just delete your bin/ directory in rails and run:

generates binstubs for ALL gems in the bundle

bundle install --binstubs

generate binstubs for a SINGLE gem (recommended)

bundle binstubs rake

rails 5

To reset the binstubs, just delete your bin/ directory in rails and run: rake app:update:bin

SideKiq specific

https://stackoverflow.com/questions/12683222/are-there-console-commands-to-look-at-whats-in-the-queue-and-to-clear-the-queue

Require the SideKiq api

require 'sidekiq/api'

get a handle to the default queue

default_queue = Sidekiq::Queue.new

get a handle on a specific queue

specific_queue = Sidekiq::Queue.new("specific")

How many jobs are in the default queue?

default_queue.size

Deletes all Jobs in a Queue, by removing the queue.

default_queue.clear

Get the stats object

stats = Sidekiq::Stats.new

Get the number of jobs that have been processed.

stats.processed

Get the number of jobs that have failed.

stats.failed

Get the queues with name and number enqueued.

stats.queues

Gets the number of jobs enqueued in all queues (does NOT include retries and scheduled jobs).

stats.enqueued

See workers

Sidekiq::Client.registered_workers

See queues

Sidekiq::Client.registered_queues

See all jobs for one queue

Sidekiq.redis { |r| r.lrange "queue:app_queue", 0, -1 }

See all jobs in all queues

Sidekiq::Client.registered_queues.each do |q|
  Sidekiq.redis { |r| r.lrange "queue:#{q}", 0, -1 }
end

Remove a queue and all of its jobs

Sidekiq.redis do |r| 
  r.srem "queues", "app_queue"
  r.del  "queue:app_queue"
end

Note: Unfortunately, removing a specific job is a little more difficult as you'd have to copy its exact value:

Remove a specific job from a queue

Sidekiq.redis { |r| r.lrem "queue:app_queue", -1, "the payload string stored in Redis" }

Delete all scheduled jobs

Sidekiq::ScheduledSet.new.clear

Delete retry jobs

Sidekiq::RetrySet.new.clear

Using redis-cli

$ redis-cli
> select 0 # (or whichever namespace Sidekiq is using)
> keys * # (just to get an idea of what you're working with)
> smembers queues
> lrange queues:app_queue 0 -1
> lrem queues:app_queue -1 "payload"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment