Sacramento Software Development

Ruby on Rails Enumerations and Fixtures

Posted by John Lockwood on June 30th, 2009

I had to figure out Ruby on Rails enumerations and fixtures today.

The solution will be here in just a moment, but first, some gratuitous background: I was working on a Person resource — or a “Contact” resource if you prefer your people to have a CRM flare to them. Naturally, hanging off of these People (or Contacts) are as many phone numbers as they might have.

Twenty-first century people have a lot of phones.

Once you start having contacts who own lots of phones, naturally you want to know if you’re bugging someone to buy your widgets on their cell phone or their home phone or what have you.

You don’t want to store whole strings to do this, so probably you end up with a line in your schema that looks something like this:


    t.integer  "phone_type"

Well, that’s fine, but in your code you don’t want to be saying “p.phone_type = 2″, because that’s not all that readable.

Now Ruby doesn’t have enums (BAD language — just for that you’re going straight to bed with no strong typing, young man). It does, however, have constants, and this Rails tip shows you how to use them.

Like me you may have opted for the simple “first approach” of adding the constants to your model, like so.


class Phone < ActiveRecord::Base
  # Type constants
  HOME = 1
  WORK = 2
  CELL = 3
  # ... more model code ...
end

Well, all that’s well and good, but then what do you do about your fixtures? Remember, your whole point in starting on this Enumeration journey was to get your code more readable, not less, so if you end up having fixtures that look like this, that’s not a good outcome:


et_the_extra_terestrial:
  id:                     1
  person_id:        1
  phone_num:     9165551212
  phone_type:     1

Phone_type: 1
At about this point, I remembered that one of my Rails books said something about dynamic fixtures. Looking those up, it was easy enough to get ET’s phone looking the way it should in no time.


et_the_extra_terestrial:
  id:                     1
  person_id:        1
  phone_num:     9165551212
  phone_type:     <%= Phone::HOME %>

And there you have it, Bob’s you’re uncle, and Drew Barrymore’s your hot aunt.

Posted in Miscellaneous | Add a comment »

Using Ruby Fixtures at the Console

Posted by John Lockwood on June 27th, 2009

At the risk of showing how (not) far I am along the learning curve given my Month of Sundays Rails project, here’s a great beginner sort of rails tip I found on how to load fixtures into the console.

The meat of it is this, which I like because it simultaneously shows you how to set up your environment correctly for the console, and for rake. Not a bad little tutorial for two lines worth of stuff:


rake db:fixtures:load RAILS_ENV=test
script/console test

Posted in Ruby on Rails | Add a comment »

Software At The Speed of Part Time

Posted by John Lockwood on June 20th, 2009

I’ve been working on a Rails application, not all the live long day, but generally on a long Friday night and part of Saturday.

Being self-employed during the boom years, now that we’re in a recession I own a real estate company for pin money, and have a full time ecological niche banging on legacy C code in a cube forest.  Following my ancestors, the hunter-gatherers, I am a typer-debugger.

On Friday nights I cut loose from the dusty streets of this one horse Bruce Springsteen song, and do, what exactly?

More typing.

Because I hope to get paid for this Rails application eventually, I have to go at it weekend after weekend.  But because I’m not yet getting paid for it yet, and because even if I ever will I’ll be more or less in charge of its direction, I enjoy it as a creative outlet.

The speed of part time takes some getting used to.   Software is one of those things where you start to really get rolling on whatever language it is you happen to be using this year after several weeks of being back in that environment.  Sure, you can be semi-productive on day one, but by week 12 you’re cruising along nicely.

When we say week twelve, though, that’s for a full time gig.  When you get a day of programming per week, then multiply everything by five, given what would have been your five day work week.  So twelve weeks becomes sixty.

Programming at the speed of part time, a six month project takes two and a half years.

To be sure, you have a bit more than a day per weekend you can conceivably put in, but on the minus side, you’re having to keep it all in memory after flushing C through your brain all week.

Needless to say, the hardest part of such a project is staying encouraged after four months or so, with a mere few weeks of progress seeming to show for one’s efforts.

It’s amazing to see how “the economy” changes things.  It seems like all the coffee shops around here are closed.  During the boom years, when one could make a living by blogging alone, I used to go out with all the other high-on-the-hog heloc bailout recipients and enjoy my French roasts.

Today I went out for my birthday mocha, and my old shop was closed, and the place I would go before I went to my old shop was closed.  To be sure, the coffee shops inside the supermarkets are still doing OK, rescued from collapse by their proximity to the produce.

I wonder if any of the old baristas who lost their jobs are now inside Safeway coffee shops, while working on their own chai latte projects on Friday nights.

I hope not.

Posted in Miscellaneous | Add a comment »

SVN Recurisvely Show Unversioned Files

Posted by John Lockwood on June 19th, 2009

Try this:

svn status | grep ?

Enjoy!

Posted in Miscellaneous | Add a comment »