Sliced Software

About

Thomas Mango writes software, has a bulldog, listens to Long Island Hardcore music and eats sandwiches.

He is currently working freelance projects as well as on his own project, Limited Pressing.

Software

PGnJ An intuitive Database Development Environment for Mac OS X.

Mobile LIRR An iPhone optimized Long Island Railroad scheduling application.

Trends An iPhone optimized Twitter Trends application. Get the current trends and their reason for trending on the go (data by What the Trend?).

A Simple .union Extension For ActiveRecord

I just wrote and released my first rails plugin, called union. It’s absurdly simple and fairly naive. I wrote this plugin so that I didn’t have to look at an ugly UNION I was running in a find_by_sql.

ActiveRecord::Base.union(parts, options = {})

The first parameter, parts, is an array of hashes. Each hash is what you would normally send into a single find and represents each SELECT. All parts will be unioned together.

The second paramter, options, is a hash of remaining options to be applied to the UNION of the parts (ie: order, limit, offset).

A simple (and useless) example would be:
User.union([{:conditions => ['name = ?', 'tom']}, {:conditions => ['name = ?', 'gary']}], {:order => 'created_at'})

This example produces the following SQL:
(SELECT * FROM `users` WHERE (name = 'tom')) UNION (SELECT * FROM `users` WHERE (name = 'gary')) ORDER BY created_at;

Essentially you can do any union, but it’s up to you to make sure you don’t pass the wrong stuff in because it’s a pretty dumb implementation.

Looking for really old posts? Until they are here at tumblr, see here!