Monday, 29 October 2012

Rails App from Scratch (On fresh Lion installation)

I'm setting up a new Mac Mini for Ruby on Rails development. It was pretty much headache free. I knew I needed my app to use MySQL so I wanted to set things up to incorporate this. Here's the list of components that need to be installed in the order shown.
  1. Xcode (with Command Line Tools)
  2. Homebrew
  3. MySQL & MySQL Workbench
  4. RVM (installing lastest Ruby 1.9.3)
  5. Install gems to against 1.9.3 gemset: rails, bundler, mysql2, activerecord-mysql2-adapter
  6. Rubymine (My editor/IDE of choice for RoR development)
Once all of these are installed, you can use Rubymine to create a new rails project, configure your database.yml file, generate a model (with a migration), and run rake db:migrate to confirm that all connections are up and running. We'll step through this at the end.

Install Xcode

Best done through the Mac App Store. Launch the App Store and search for xcode. Follow directions to install it.

Once Xcode is installed you need to install the Command Line Tools. This gets you lots of command line goodies we'll need for ruby and rails development. (One main one being the gcc compiler, integral for running Ruby.)

Within Xcode, go to Preferences > Downloads > Componenents. From here, you can install the 'Command Line Tools'.

Install Homebrew

Homebrew is a package manager for Mac OSX. It's currently quite popular and I like it as it seems to work the most smoothly with the latest components new in Lion. Here's a comparison between the different options in this area.

Lion ships with Ruby installed, so we can use this default version to install Homebrew. Run this in your terminal: 

$ ruby -e "$(curl -fsSkL"

Install MySQL

Super easy now that we have Homebrew.

$ brew install mysql

When this finishes it gives you some instructions of a few things to do to set up the root account and different configuration options for startup, etc... read these and do what you think you need to do. I'm going to configure it to start when the system starts

$ mysql_install_db --verbose --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.5.15/com.mysql.mysqld.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

You may still need to manually start it at this point before you can log in.

$ cd /usr/local/opt/mysql ; /usr/local/opt/mysql/bin/mysqld_safe &

Now, login.

$ mysql -uroot

If this is successful, you're almost there, the only thing left is to install the MySQL WorkBench!

Create a connection using the default configurations: user: root, host:, port: 3306. Now you can create databases against this connection through this interface. We'll do this later though!

Install RVM

I used this as a step-by-step installation guideline.

But, basically... for installation. You just need to run this from your terminal:

$ curl -L | bash -s stable

Now we can install the latest Ruby (or which any you need) First, list all the ruby versions available for installation:

$ rvm list known

I'm going to install the latest version, 1.9.3.

$ rvm install 1.9.3

Before we start installing gems, lets tell RVM the version of ruby we want to install gems against.

rvm use 1.9.3

Installing Gems for Rails Development using MySQL

$ gem install rails
$ gem install bundler 
$ gem install mysql2
$ gem install activerecord-mysql2-adapter

Install RubyMine create a new Rails project

Once RubyMine is installed, launch it and create a new project. 
  • Make sure to specify the project type as 'Rails Application'.
  • Select the ruby version you want to use. 
  • If RubyMine doesn't know the Rails version it should use, you will need to click the '...' box next to the Rails Version dropdown. RubyMine will go and get it!
  • Click 'ok' and we're ready to go!

Configure your database.yml File

It should look something like this:

Test that everything is up and running.

$ rails g model user
      invoke  active_record
      create    db/migrate/20121029132052_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/unit/user_test.rb
      create      test/fixtures/users.yml

Edit your migration file to reflect the columns you'll need in your table. If all this doesn't make sense, spend some time researching ActiveRecord and Rails database migrations.

If you need a cheat sheet to help with defining your columns, check this out.

Now you should be able to run this:

$ rake db:migrate

BOOM. Easy Peasy.

No comments:

Post a Comment