Don’t serialize ActiveRecord objects

ActiveRecord provides a convenient interface for serializing Ruby objects. In the model file, simply declare which column will store the object:

All seems fine until you actually have to retrieve some of the data:

This isn’t the exact code situation I was in, but it’s similar enough.
Here’s how I solved the problem:

The attributes method call returns a hash of the objects attributes and values. This is a much more reliable way to serialize a Ruby class with ActiveRecord.

The perils of syntactic sugar in Ruby

I ran into this doozy today:

In math, a+b always equals b+a. Not always the case with Ruby and Time objects.

Trivial Rails tip: easy html spaces

For when you don’t want to type   a dozen times, add this to application_helper.rb:
def nbsp(n=1)
    n.times.map {" "}
end

In a view:
<%= nbsp 5 %>
replaces
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Managing development databases

Today I asked this question on StackOverflow:

My development team of four people has been facing this issue for some time now:

Sometimes we need to be working off the same set of data. So while we develop on our local computers, the dev database is connected to remotely.

However, sometimes we need to run operations on the db that will step on other developers’ data, ie we break associations. For this a local db would be nice.

Is there a best practice for getting around this dilemma? Is there something like an “SCM for data” tool?

In a weird way, keeping a text file of SQL insert/delete/update queries in the git repo would be useful, but I think this could get very slow very quickly.

How do you guys deal with this?

I got many responses very quickly. When the dust settles, I’m going to write an update with my favorite answers. Until then, feel free to check out the page for yourself–or if you’re up to it, post an answer.

Update: Rob Shedd posted the question on Hacker News. Give that link a shot if you prefer HN over StackOverflow.

FanGamb update

FanGamb is undergoing some exciting changes. Co-founder Rob Shedd writes a bit about it here:

We have some exciting things in the works and as a result, will be moving to Europe later this summer to continue developing our product and business. We can’t yet share all of the details on why we’re making the move, but I’ll share more as soon as I’m able.

Setting up Ruby on Linux

Up until now, I’ve done nearly all of my Rails programming on Windows Vista (64 bit). The environment has been pretty good, but it’s time for a change. This was probably due a while ago, but I’ve decided to explore Linux with a dual boot of Ubuntu 10.x.

Thanks to StackOverflow and some Googling, I got mostly up and running in about a day. Here’s a summary of some the steps I took to hopefully help others in the same position as me: decent at programming, but terrible at Linux.

  1. Get Linux working alongside Windows
  2. There are two ways to do this (that I know of): Dual boot and Virtual machine. With a dual boot, you get to choose at startup which OS you want to run. With a Virtual Machine (VM), one OS is like a program running in the other. For example, I could easily alt+tab between Linux and Windows. I went the dual boot route by using a program called Wubi. For VMs, Virtual Box is highly recommented (I almost got it working). I went with Ubuntu 10.something for no real reasons other than it’s the newest version of Ubuntu and that I’ve read that Ubuntu is easy to use. Follow the Virtual Box wizard and you should be in good shape. (StackOverflow)

Continue Reading »