blog Code meaningful labor rails ruby

Add automatic URL generation to your Rails App (+ a little about ActiveAdmin)

It’s easy to add a “url” or “slug” field to your rails app, and override the to_param function to report that field. A route can then look for an alphanumeric parameter, like this:

match "pages/:slug" => "purchases#show_by_slug"

But how to generate them without too much manual labour? [manual labour is a good thing, relying on well written gems is sometimes better]

blog Code giants heroku rails

Reducing Heroku Cost with Asset Pipeline Sync

GiantsLive which is hosted on the new heroku Rails 3/Cedar platform a single ‘dyno’. If you don’t know what heroku is, essentially it’s a cloud web hosting platform, and one of it’s finest features is that your first 750 ‘computation hours’ in a month are absolutely free. Properly free, like a lunch at that church on Stranmillis Road on a Thursday.

Code learning rails ruby

ActiveAdmin on Heroku (Rails 3.1)

I wrote a post detailing some issues I had with ActiveAdmin and Heroku with Rails 3.1.

It’s probably entirely out of date, but in case its of any use for you, it’s still archived here.

blog meaningful labor rails

Site Launch — Tales of the Unexpected

Last week saw the launch of TOTU: Tales of the Unexpected, a Summer Madness and Exodus project. The site was developed by me with visual identity design by Connie & Craig, the super-talented folks over at beautiful end product.

“TOTU is an opportunity for everyday people to express a part of their lives, a snapshot of faith, or quite simply, an encounter of God – and that’s always worth talking about”

I hope you enjoy watching the videos and maybe feel inspired to share.

So, the site is now live. Enjoy!

blog Code learning rails

Ruby on Rails: Get some random records

EDIT AGAIN: Much better..

To get a ‘num_reqd’ array of random objects, you can use something like this.

  named_scope :large, :conditions => ['image_file_name IS NOT ?', nil]
  named_scope :small, :conditions => ['small_image_file_name IS NOT ?', nil] 

  def self.get(num_reqd,features_arr=[],size="large")
    if size=="small"
      collection = Feature.small
    elsif size=="large"
      collection = Feature.large
    return collection if collection.size <= num_reqd

    # num_reqd.times{feature=self.random(collection); features_arr.push(feature) unless features_arr.include?(feature)}
    features_arr = collection.find(:all, :limit => num_reqd, :order => 'rand()')

    if features_arr.size < num_reqd
      return Feature.get(num_reqd, features_arr, size)
      return features_arr

EDIT: It's much cleaner and easier to use something in the form below, though the following is probably useful in some cases and is possibly interesting as a code snippet.

User.find(:all, :order => 'rand()')

---- end edit.

Working from a baseline of the code found here at I've extended a 'random record grabber' to get a specific number of unique records from a Rails data table.

Basically - the random method makes a database call to get the ids of a table, and sends back a random entry. self.get is a recursive method which provides a 'total number required' and a base array to start from (if you wish to specify entries to appear in the otherwise 'random' list). First year computer science should help get your head around the rest!

def self.random
    ids = connection.select_all("SELECT id FROM features")
    find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?

  def self.get(num_reqd,features_arr=[])
    num_reqd.times{feature=self.random; features_arr.push(feature) unless features_arr.include?(feature)}

    if features_arr.size < num_reqd
      return Feature.get(num_reqd, features_arr)
      return features_arr

Be aware, there is more efficiency to be found in the database call (i.e. it should be cached). Also, you'll want to be sure there are at least 'num_reqd' items in the database.

learning meaningful labor rails ruby

Simple Thinking Sphinx on Dreamhost

*** Please note – this will probably not work (at all) (for more than a day of light use) without Cron use. And isn’t at all authorised by Dreamhost!! ***

For a recent client project I’ve used a Dreamhost unlimited account, which for value compared with the resources available and the fact that you don’t have to do any building or setting up of the server environment makes it an easy win for a site that’s not going to have a huge amount of traffic or a large amount of processing.

Post-launch I got to work putting together a basic search engine and here’s a quick run through of the steps it took to get a very simple Sphinx instance working on Dreamhost, and a few hurdles thrown in the way by various googled articles.

Development Environment

Using the guide from FG install Sphinx locally:

curl -O
tar zxvf sphinx-0.9.8-rc2.tar.gz
cd sphinx-0.9.8-rc2
sudo make install

then install the TS plugin into your application

script/plugin install git://

Any problems with that, check out the FG page linked.

Getting a basic search going

Following tutorials such as the Sphinx Railscast will get you there pretty quick.

In your searchable model you need to define an index

class Page < ActiveRecord::Base
  define_index do
    indexes :title
    indexes :long
    indexes :short
  end ...

Run the indexer and start the Sphinx instance:

rake thinking_sphinx:index
rake thinking_sphinx:start

After this you'll be able to search on your object. So using script/console

@searched_pages ="query")

will return what you're looking for!

Setting up Dreamhost

First things first you need to install Sphinx in your local area, as posted by Hugh Evans:

cd ~/
mkdir -p local
tar -xzf sphinx-
cd sphinx-
./configure --prefix=$HOME/local/ --exec-prefix=$HOME/local/
make install

then set up the PATHs

echo "export PATH="$PATH:~/local/bin"" >> ~/.bash_profile
source ~/.bash_profile

You can choose to set up a CRON task at this point too, but I'm not going into that.

Also at this point in the there's talk of using Sphinx being anti TOS in DH's eyes... but we'll see does the process get killed or not!

Configuring Sphinx for DH

Create a file called sphinx.yml in the RAILS_ROOT/config/ folder.

Because Dreamhost uses an externally referenced MySQL server instead of localhost you need to set up the sql_* parameters:

  sql_host: "mysql.YOURDOMAIN"
  sql_port: 3306
  sql_user: "USER"
  sql_password: "PASSWORD"
  sql_database: "DATABASE"

And because you installed Sphinx in your local area:

  bin_path: '/home/YOURUSERNAME/local/bin'

Finally, after setting whatever memory/fine tuning settings you wish/require set up the locations for the Sphinx files:

  config_file: "/home/YOURUSERNAME/"
  searchd_log_file: "/home/YOURUSERNAME/"
  query_log_file: "/home/YOURUSERNAME/"
  pid_file: "/home/YOURUSERNAME/"
  searchd_file_path: "/home/YOURUSERNAME/"

That should be you ready to start deploying.


Using Git + Capistrano for deployment (and Passenger for the http server) my deploy.rb's namespace area looks like this:

namespace :deploy do
  task :restart do
    run "touch #{deploy_to}/current/tmp/restart.txt"
  task :start do 
    # nothing  (this avoids the 'spin' script issue)
  desc "Re-establish symlinks"
  task :after_symlink do
    run <<-CMD
      rm -fr #{release_path}/db/sphinx &&
      ln -nfs #{shared_path}/db/sphinx #{release_path}/db/sphinx
  desc "Stop the sphinx server"
  task :stop_sphinx , :roles => :app do
    run "cd #{current_path} && rake thinking_sphinx:stop RAILS_ENV=production"

  desc "Start the sphinx server" 
  task :start_sphinx, :roles => :app do
    run "cd #{current_path} && rake thinking_sphinx:configure RAILS_ENV=production && rake thinking_sphinx:index RAILS_ENV=production && rake thinking_sphinx:start RAILS_ENV=production"

  desc "Restart the sphinx server"
  task :restart_sphinx, :roles => :app do


There's probably a neater way to do this, but basically this makes sure Sphinx's indexes and conf files live in the shared deployment folder.

I recommend you try all this in a staging area first, obviously... and you can use Dreamhost's control panel to set up a staging subdomain with a new database in whatever fashion you prefer.

Any problems with this script flag them up, please! This is as much for my future reference as you googlies out there.

blog meaningful labor rails ruby

Rails / LAMP / WordPress Setup Guide

I’ve had huge amounts of pain getting WordPress set up locally over the last few weeks on my old machine and when it struck again with my new development environment it definitely time to write down the required steps to get a great LAMP/Rails setup prepared (for me)!

Leopard has PHP and Apache fairly up to date, and probably MySQL as well, but I decided to get MySQL up to scratch using the Universal Binary along with a few command line instructions available here (guide and links to downloads).

I didn’t use this script myself, and at a year old it may have a few imperfections, but HiveLogic have a fairly well rounded Ruby/Rails install going on.

Finally, WordPress to go on your LAMP stack will be a lot less painful using this guide for Tech Recipes.

Any suggestions additions or replacements for this list? Drop a comment!

blog rails

It’s been quiet around here

The Geeky Irish Rockstars I have six draft posts in the wings waiting to be pushed out, works in progress related to technologies I’ve been working with lately for

Working on a still-hush-hush project with the lads best known for their rock star company photo and probably this week for their choice of marketing for Exceptional, the bug tracking tool for Rails (and more) at Railsconf in Las Vegas.

Part of the reason there’s been so little going on here is that I’ve been… well… rather busy. Trying to get client work done as well as freelancing with Contrast has been a bit of a struggle and I’ve just spent three days getting my brand new 24″ iMac set up and running… a pain worth the endurance, though I’m fairly certain I now need a bigger desk!

So, some posts about Rails, HAML, Cucumber, and possibly a very very late review of FOWA Dublin which never quite got finished coming up in the next few weeks.

blog meaningful labor rails ruby

Git Pro Tips!

Stumbled across this fella on Twitter today, noticed he worked at Slide (who make successful/annoying Facebook Apps). None the less (I joke) he writes a blog at and has recently been writing a series of ‘ProTips’ on using Git Repos for his work colleagues.

I think it might prove useful: will be taking a read in my spare time.

learning meaningful labor rails

Ruby Uninitialized Constant Error (Mental Note)

The reason I always get initialized constant errors occurring in Ruby is accidentally naming models the plural form of the database table name and not letting Rails automatically create the pluralisations.

script/generate scaffold contact_skill contact_id:integer skill_id:integer description:string
script/generate scaffold contact_skills ...

(This reason is one I constantly forget, and Google never helps me. Now I will not forget)


edit: I’ve had a LOAD of google hits from this search query in the last few days, if this helps you or doesn’t please leave a comment to make this article more complete.