Handy Ruby on Rails Tip

When using a few different Unix environments for development, the location of the socket for MySQL tends to vary quite a bit. Having to keep your database config different from the tree is a pain.

Luckily, there is a neat, but not widely known fix for this issue. It turns out that the YAML files used for Rails configuration are actually passed through the ERB. Now I can use this and it works without modification:

  adapter: mysql
  database: development
  username: root
  host: localhost
  socket: <%=
     ['/opt/local/var/run/mysql5/mysqld.sock', # darwinports
      '/opt/local/var/run/mysqld/mysqld.sock', # darwinports, again
      '/var/run/mysqld/mysqld.sock',           # ubuntu/debian
      '/tmp/mysql.sock'].select { |f| File.exist? f }.first %>

yes, I know this isn’t mindblowing, but seriously… in the real world of development… isn’t it nice to get rid of the nits that pile up and frustrate everyone.

This entry was posted in General. Bookmark the permalink.

One Response to Handy Ruby on Rails Tip

  1. Giao Phan says:

    Interesting, I actually just hit this when I was hacking on the plane, my solution was actually just to drop the socket line altogether and let rails use TCP instead of domain sockets. I figured for development it wasn’t such a big deal. Pretty cool find though.

Leave a Reply

Your email address will not be published.