Generating Entity-Relationship Diagram for Ruby on Rails

Install graphviz (if not yet)
linux sudo apt-get install graphviz
osx brew install graphviz

Add the gem

In the project gemfile add

group :development do
  gem 'rails-erd'

Then execute cd PATH_OF_THE_PROJECT && bundle install

Using it
It's as simple as a bundle exec rake erd and it will generate a PDF file in the root of the app.

Rails 4.1.0 - Nginx Bad Gateway with Passenger

I'm writing a little issue tracking application, and as it's a new app I'm using the new shiny Rails 4.1.0. However, whilst the application runs fine locally using Webrick I've been unable to get it working when deployed onto a live (staging) environment running Passenger and Nginx (using the Ubuntu package available via the Phusion Passenger site).

When deployed and configured, Nginx reports a 502 Bad Gateway exception. Digging throught the Nginx error log (in /var/log/nginx), the error becomes clear:

Exception RuntimeError in Rack application object (Missing secret_key_base for 'production' environment, set this value in config/secrets.yml)

Opening secrets.yml shows that there is no value for production:

  secret_key_base: **not_for_prying_eyes**

  secret_key_base: **not_for_prying_eyes**

# Do not keep production secrets in the repository,
# instead read values from the environment.
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

You can either set an environment variable, or directly configure a value here (you can run rake secret to generate a key). Just make sure that if you do the latter, you've added secrets/yml to your .gitignore!

bundle install --deployment

หาก Gemfile ใช้ gem ที่ยังไม่ใช่ .gem เช่น


gem 'prawn', git: '', branch: 'master'

*** อย่าลืมลง git ด้วย ถ้า source เป็น git

bundle install ก็ผ่านปกติ แต่...

Error message: (at master) is not checked out. Please run `bundle install` (Bundler::GitError)


bundle install --deployment

RAILS MySQL ssh Tunnel

มีโทรศัพท์แจ้งมาว่า "แบท พี่เปลี่ยนวิธี connect mysql ให้ต้อง ssh tunnel นะ"
ตึ่งโป๊ะ!!! ทำไงๆๆๆ คืออะไร แค่เคยเห็นผ่านๆ แล้ว Ruby on Rails ทำไง

ssh -N -L 8888: -f remote-user@remote-domain-or-ip

ใน database.yml ก็

  adapter: mysql2
  encoding: utf8
  port: 8888
  database: databasename
  username: remote-user
  password: password-remote-user

เสร็จ connect สำเร็จ ข้อมูลมา แต่ทำไมออกมาไม่เหมือนกัน
อ้าว!!! เปลี่ยนชื่อ database อีกก็ไม่บอก

Send backup by email with crontab

echo "This is the message body" | mutt -a "/path/to/" -s "subject of message" --

So probably the approach to follow will be something like,

tar -zcf /home/blah/backup.tgz /home/blah/
echo "Please find attached the backup file" | mutt -a "/home/blah/backup.tgz" -s "File attached" --

I will save the above script as and schedule the cron job as,

0 1 * * * /path/to/