NanoXD’s

Random Ramblings

Rails 4: Easily Add Reference to Migration

While working on a Rails 4 application, I came across a new addition to migrations add_reference. add_reference makes it easier to create migrations for foreign keys.

Rails 3

In Rails 3, to add a foreign key and an index you would write a migration like this:

1
2
3
4
5
6
class AddUserToBooks < ActiveRecord::Migration
  def change
    add_column :books, :user_id, :integer
    add_index  :books, :user_id
  end
end

It requires two method calls, one to add the user_id column to the Books table, and another to add an index to user_id.

Rails 4

In Rails 4, you can accomplish the same thing with add_reference:

1
2
3
4
5
class AddUserToBooks < ActiveRecord::Migration
  def change
    add_reference :books, :user, index: true
  end
end

While it isn’t a major change, it further illustrates the foundation that Ruby was built upon. “Ruby is designed to make programmers happy.” – Yukihiro Matsumoto


How to view open connections on files

During a pairing session on Ruby, we had an issue with what file the API was stuck on. Luckily, there is a command lsof which stands for list open files.

Usage

When lsof is ran without parameters, it will show all the files opened by any process. It can get a bit messy, so I recommend greping through to find the file/process you want.

To find out what files Ruby has opened you can use lsof -c. Here is what it looks like when running rails server:

1
2
3
$ lsof -c ruby
ruby    55222 nano   10w   REG    1,1   1214625 34875337 ~/Developer/Projects/contact-manager/log/development.log
ruby    55222 nano   11u   REG    1,1     49152 34875460 ~/Developer/Projects/contact-manager/db/development.sqlite3

To find out what is accessing any file in a directory, use the lsof +D /usr/directory

1
2
$ lsof +D /Users/nano/Developer/Projects/contact-manager
vim     44957 nano  cwd    DIR    1,1      748 34873022 ~/Developer/Projects/contact-manager

You can list all opened internet sockets using lsof -i.

1
2
3
4
5
6
7
$ lsof -i :62658
Rdio      63484 nano   23u  IPv4 0x2674679fdf954c9b      0t0  TCP 10.0.1.14:62658->8.18.203.81:sunproxyadmin (ESTABLISHED)
# We can find out the process id for Unicorn running on port 8080
$ lsof -i :8080
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby    54171 nano    9u  IPv4 0x2674679fe1b36243      0t0  TCP *:http-alt (LISTEN)
ruby    54172 nano    9u  IPv4 0x2674679fe1b36243      0t0  TCP *:http-alt (LISTEN)

After finding the process id, you can find out what that process is accessing using lsof +p PID

1
2
3
4
5
6
7
8
$ lsof +p 79766
COMMAND   PID USER   FD   TYPE             DEVICE  SIZE/OFF     NODE NAME
ruby    54172 nano  cwd    DIR                1,1       748 34873022 ~/Developer/Projects/contact-manager
ruby    54172 nano  txt    REG                1,1      9184 17640590 ~/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
ruby    54172 nano  txt    REG                1,1   2793108 17640592 ~/.rvm/rubies/ruby-2.0.0-p247/lib/libruby.2.0.0.dylib
ruby    54172 nano  txt    REG                1,1    118244   615195 /usr/local/Cellar/libyaml/0.1.4/lib/libyaml-0.2.dylib
ruby    54172 nano  txt    REG                1,1    378152   797957 /usr/local/Cellar/openssl/1.0.1e/lib/libssl.1.0.0.dylib
ruby    54172 nano  txt    REG                1,1   1663200   797956 /usr/local/Cellar/openssl/1.0.1e/lib/libcrypto.1.0.0.dylib

Using several commands leads you to any files that may be stuck in processing. I have found that it is an essential tool in debugging a multitude of issues.

References


Make iTerm 2 work with Mac OSX movement keyboard shortcuts

After transitioning from Terminal to iTerm 2, you may notice commands like ⌥ + ← (moves left one word) and ⌘ + ← (moves to the beginning of the line) no longer work.

It is very simple process to add the keyboard shortcuts to your profile. Here are the keyboard shortcuts we are adding:

Keyboard Shortcut Description
⌥ + ← Move one word to the left
⌥ + → Move one word to the right
⌘ + ← Move to the beginning of the line
⌘ + → Move to the end of the line

1. Open Preferences

2. Click on Profiles

Find the Keys header and click on the + sign to add a new shortcut key.

3. Add Keyboard shortcuts

Enter each of the following shortcuts below:

Congratulations, you can now move around in iTerm 2 just like Terminal!