Tag: Ruby On Rails

January 2012 Ruby on Rails Stack

Posted by – January 10, 2012

It seems like I am, entirely too frequently, setting up a new development environment for Ruby on Rails. Between the computers at work and at home I feel like I am starting over, from scratch, somewhere every 6 months or less. Whenever I make a substantial change to my development environment, I like to document it here for my own reference and in the hopes that it may help others.

This go around I am incorporating some recent additions to the stack, notably Homebrew and rbenv. These additions, as well as a few other changes are being made to simplify things and to follow the latest best practices.

The steps I followed to setup my development environment are as follows:

1. Install Homebrew – the missing package manager for OSX. Luckily I didn’t run into any issues installing Homebrew. Homebrew greatly simplifies installing some of the more difficult bits and pieces on OSX.

2. Install rbenv – This differs from my previous use of RVM. While I really liked RVM, the simplicity of rbenv convinced me to switch. Again, no real issues with installation (that is the beauty of a single purpose, simple tool). I didn’t use Homebrew to install rbenv, but in the future I will. With rbenv I also installed the companion ruby-build which simplifies the process of installing various Ruby versions.

3. Install PostgreSQL – While I am a longtime fan of MySQL, it’s acquisition by Oracle combined with the herd moving towards Postgres made me take the plunge. All in all it has been a fairly straightforward process. There is one caveat, when installing Postgres on OSX Lion (which replaced it’s bundled install of MySQL with Postgres) you need to make a minor change to the system paths so that Postgres loads from your installation and not the built in one. I followed the tips from a few blogs and updated /etc/paths, placing “/usr/local/bin” at the top. That one tip, combined with following the instructions provided by Homebrew (accessible via “brew info postgresql”) will get Postgres up an running in no time.

That is basically it! I think this is a really clean and simple install that makes my apps fairly portable. I am using Bundler to manage my Ruby Gems in conjunction with the rbenv-gemset plugin. I could just rely on Bundler, but I prefer to have the gems sandboxed in a separate gemset (for now).

After cloning from GitHub and running “bundle install” and the various rake database tasks I was up and running in development mode with relative ease. One of the wonders of the open source software community is that there seems to be a real emphasis on continuous and meaningful improvement. With each major iteration the tools I am using are becoming both more powerful and easier to use. I was able to get my development environment up and running in about an hour (less if my memory were better). That pales in comparison to some of the herculean efforts required in the past.

Installing tools for Rails development

Posted by – March 1, 2010

I have long been a fan of the various tutorials written by Dan Benjamin at Hivelogic for installing the various components of a Ruby on Rails development environment. Some of these articles are:

Recently, however, I have found a new way to install Ruby, RubyGems and Rails. This new method allows for a simple installation and switching between different versions of Ruby. Enter, Ruby Version Manager (aka RVM). Once installed and configured, installing Ruby is as simple as:

rvm install 1.9.1

I was introduced to this magical tool via a screencast from Ryan Bates on Railscasts entitled: Rails 3 Beta and RVM

Ruby on Rails, Passenger & MySQL PATH issues in Snow Leopard

Posted by – September 8, 2009

I recently did a clean installation of Snow Leopard, which is great by the way, and I ran into some issues after installing MySQL and Ruby on Rails based on the instructions from Dan Benjamin at Hivelogic.

Specifically, when I would try to access a site in development mode I would get an error from Passenger that “Rails 2.3.4 could not be found”. I also noticed that on reboot my PATH wasn’t being loaded properly either so the system could not find MySql, Ruby, or RubyGems.

I tried placing the path information that, according to the instructions goes in ~/.profile into my ~/.bash_profile.

Now when I restart the system, MySQL and Ruby on Rail can be found! Passenger is working now as well. I am not sure if there was a change in the way Snow Leopard loads bash profiles or if there is just a problem with my installation that was preventing it from working when the PATH was specified in ~/.profile, but it’s all working now so hopefully this will help someone else.

Make OSX Terminal pretty

Posted by – August 31, 2009

I have always been annoyed with how a new tab opens in the OSX Terminal. I have my Terminal set to run “source ~/.bashrc” when the application starts. The problem with this is that it won’t run for new tabs, and I use lots of tabs (Rails development, web server, database, ruby console, etc…). And while I have been annoyed, it never really bothered me enough to look for a solution, until today.

As is usually the case, a quick search in Google led me to “Customizing the terminal prompt on Mac“. Apparently, OSX handles the terminal profiles a little differently than what I am used to. OSX uses profiles, where Ubuntu uses .bashrc. So, just add one line in your terminal profile to tell the terminal run the necessary command to load your .bashrc like so:

  1. First open up your ~/.bash_profile in your text editor of choice
  2. Add the line “source ~/.bashrc”
  3. Save and close ~/.bash_profile

Easy as can be! Now set your .bashrc to make your terminal as color-coded and informative as you want it to be. There are a lot of tutorials and information available for editing your .bashrc file.

Updating Ruby on OSX Leopard

Posted by – May 13, 2009

Quick and easy method for updating, or rather installing an updated version of Ruby on OSX Leopard (10.5) from Hivelogic. Ruby ships with OSX Leopard which is great, but the version included is outdated in regards to the recommendations of the Ruby on Rails folk.

This method seems to be the best way because it doesn’t overwrite what Apple has shipped with OSX, rather it just tells the computer to keep the older version, but use the newer version in it’s stead. Of course, this does require some command line shenanigans, but once you get started it really isn’t all that difficult. Besides, if you are concerned about using Ruby and/or Rails you should be comfortable using the command line!

Some Git goodness

Posted by – May 11, 2009

A project I am working on has a configuration file that contains a few variables for using Microsoft’s Active Directory to authenticate to a Ruby on Rails application. This is working great, but I was banging my head against a wall trying to get Git to not track changes to the configuration file. I didn’t want my username and passwords tracked in GitHub for obvious reasons, but I did want a generic copy of the configuration file in the repository.

While searching for a solution today, I ran across this little gem of an article titled “GIT: ignoring changes in tracked files” from Pagebakers.nl, the salient code is below:


git update-index --assume-unchanged <file>

Add weekdays in Ruby on Rails

Posted by – March 9, 2009

I am working on an application where I need to be able to add 3 week days to today’s date. Unfortunately there doesn’t seem to be any built-in functionality for this in Ruby, so I needed to roll my own. At first I searched Google hoping someone had already solved the problem for me, which is most often the case. Not so with this problem. I did however come across this post on Platte daddy, which provided a good starting point, but was designed to simply add 1 weekday, not any number of days that I want.

I ran through a number of iterations until I came up with this helper method:


def add_cwdays(date, n)
#Add number of weekdays plus necessary weekend days
date += n.days + (2*(n/5.floor)).days
#If the above lands on a weekend, keep adding days until it is a weekday
date += 1.days until (1..5).member?(date.wday)
date
end

This was added to my ApplicationHelper.rb file and is called for adding 3 week days to today via:


add_cwdays(Time.now, 3)

I created a helper to DRY the method and I wrote it so that the starting date and the number of workdays is variable. The helper has only 2 lines of code (after some re-factoring), which reduces the readability a bit, but it still makes sense I think.

The first line simply adds the desired number of weekdays, plus any weekend days that would be within the range of week days. If we wanted to add 8 weekdays, we would need to add 10 total calendar days (1 weekend). If we wanted to add 23 weekdays, we would need to add 31 total calendar days (4 weekends). If we wanted to add 4 weekdays, we would need to add 4 total calendar days (no weekends). The weekend addition is made with the (2*(n/5.floor)).days bit of code. Basically take the number of days “n”, divide by five, strip off the decimal and multiply by 2 (for Saturday and Sunday).

The second line of code deals with the eventuality of the first line landing on a weekend. If this happens, we simply add 1 day repeatedly “until” we reach a weekday.

This seems to be working well, if anyone looks this over and sees anything wrong or if there are any opportunities to re-factor and simplify, please post comments.

Linux server instructions

Posted by – January 20, 2009

Slicehost has some really nice tutorials and instructions for setting up a Linux server to get a website up and running. I just followed the tutorial for setting up a Ruby on Rails site being server by Apache using Passenger. If this sounds like a foreign language, well the names for software in the FOSS world can be a bit confusing but once involved in the process they become more familiar.

There are four basic steps that I followed:

1. Install and configure the server’s operating system (parts 1 & 2, Ubuntu 8.10 (aka Intrepid Ibex)
2. Install Ruby, Ruby Gems and Rails
3. Install Apache and Passenger and configure them to serve up a Rails site.
4. Make a website

Not too difficult, well, except for step #4! Ha! All told, the entire process takes about an hour (most of that spent downloading, installing and updating the software).

I think I Git it

Posted by – October 28, 2008

I just used Git for the first time and I think I finally understand what all the hullabaloo is about. Git is a distributed source control tool being used by the Linux and Ruby on Rails community.

The difference between Git and other source control systems like Subversion is that there is no need for a central repository for your source code. Your code remains completely distributed amongst the various machines and people that are working on it.
I currently use Subversion to maintain my source code, but I am thinking that I may like Git better.

UPDATE: To install Git on Mac OSX (10.5) I used the following, graciously taken from this forum post at Slicehost.

First, get the source code for the latest stable build here, then:

sudo apt-get build-dep git-core
tar xjf git-1.5.5.1.tar.bz2
cd git-1.5.5.1/
./configure
make
sudo make install

Ruby & Rails using Passenger

Posted by – July 28, 2008

I have been toying with a MySQL server at work for loading data from a legacy database and creating user accounts in Microsoft’s Active Directory. I am using linux along with MySQL, Ruby and the Rails framework. While I am certainly no über-expert at this, I am finding the task not only manageable, but altogether pleasant. My current workflow looks like this:

  1. Mount Windows share using Samba
  2. Load data into MySQL and perform a bunch of transformations on it
  3. TODO: script creation of Active Directory accounts from MySQL Database (Python??)

I found that I needed to view the data as I was testing the MySQL transformations, so I turned to Ruby and Rails. I have not had a great deal of luck in the past with configuring Apache to serve up dynamic content via Mongrel or lighty, so I decided to give Passenger a go. For those of you that are unaware, Passenger works directly with Apache and requires almost no configuration. It is dead simple to use. So, within a matter of minutes (minus the time to tweak my database to conform to the Rails conventions) I had a web site up and running to display my data as I was testing it!

Open Source Software is becoming (for me at least) a more viable solution to the problems I face than the proprietary alternatives. The wealth of information available and the relative ease with which a great deal of work can be done is amazing.