Boban Jovanoski

Some Tips And Tricks BobiYo Jan 28

1 comment Latest by Olive

Tags: rails   console   ubuntu   tip   trick  

List Rake Routes

rake routes | sed -e "1d" -e "s,[/]*,,g" | awk '{print $1}' | sort | uniq

Remove old kernel versions - Ubuntu

I'm using Ubuntu and RubyMine for developing ror apps and after some period of time after many ubuntu updates, I feel the need to remove old kernel versions :) and this is the command I use (10x @gemidjy):

sudo apt-get remove --purge `dpkg --get-selections 'linux-*.*' | awk '$2 == "install" { print $1 }' | grep -v "$(uname -r | sed 's/\(.*\)-.*/\1/')"`

Which process is using your port?

Sometimes you can't start a server/service because the port has been already taken? - In the example we search which process is using port 3000. You can check that with:

netstat -nlp | grep 3000 
#OR
fuser -n tcp 3000

List previous commands in rails console

While developing a rails app and meanwhile experimenting in the console :), sometimes you write a bunch of stuff which are chained and hitting the up arrow can sometimes be hurtful so using this command will show you a list of your previously entered commands:

puts Readline::HISTORY.entries.split("exit").last[0..-2].join("\n")

Use last result

After you execute some command in the console lets say User.first and you forgot to put it in a variable, instead of (up arrow, home, add variable name =) you can simply write:

user = _
# _ is the result of your last command :)

SQL to Rails Console

The simple way of doing this is after starting the rails console:

ActiveRecord::Base.logger = Logger.new(STDOUT)

If you want your console to be colorful and you can see all the db queries in all your rails project without writing this all the time after starting your console, you need to add changes to your .irbrc file in your home directory. I haven't added my .dotfiles on github yet...I'll update this post and set up a link after I do that :), meanwhile you can try putting this in your .irbrc file (It's old code that I found in my tips_and_tricks.txt file - email me or comment for an update I'll gladly change it. There is a gem for doing this but I forgot which one was the coolest).

script_console_running = ENV.include?('RAILS_ENV') && IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
rails_running = ENV.include?('RAILS_ENV') && !(IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
irb_standalone_running = !script_console_running && !rails_running
if script_console_running
   require 'logger'
   Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end

Routes In Console

To use routes methods in your rails console you need to include and set:

include ActionController::UrlWriter
default_url_options[:host] = 'whatever'

Listing controller routes

I got this tip from my friend Dalibor. You can list routes for a single controller by running:

rake routes CONTROLLER=projects

I hope that some of the tips help. Feel free to comment, add suggestions, etc.