From the monthly archives:

July 2009

I work for a lead generation company. Part of the application I’m working required us to know how many people viewed a particular item in the system. So, my co-worker and I created an Impression model that tracked each appearance of the associated object. We created a polymorphic association to allow impressions to be gathered on several different models.

The trick was that we found ourselves writing the same functionality on each of the models that needed impressions gathered. So, we took that functionality as well as the has_many polymorphic association and moved them into a module. Here’s what the module looks like:

[click to continue…]

{ 7 comments }

When I first started playing with HTML, the way you centered your HTML elements was to place a <center> tag around them. If you try using a <center> tag in your HTML in any type of IDE, you’ll get a deprecation warning. (If you’re new to programming, deprecation means that the functionality is slated to be phased out.)

So, what do you do instead? Well, there are two instances in which I was wishing I knew how to get the same effects I got from <center>: centering text and centering elements.
[click to continue…]

{ 4 comments }

A lot of new Ruby developers I’ve worked with have seen the symbol notation—starting with a :— and have been confused by what a symbol actually is. There is a lot of information out there that is confusing as well. Here’s a brief rundown of what symbols are and how they are used.

[click to continue…]

{ 3 comments }


This is a walkthrough on how to create a simple address book using Ruby on Rails v2.1.

{ 18 comments }


Presentation given to introduce Rails for URUG Rails Workshop 2009.

In this screencast, you can learn about Rails from an outside-in approach.

{ 14 comments }

I had a friend email me a week or so ago about generating a link from your restful routes when you don’t know the class. Here’s a quick thumbnail of the scenario.

You have a bunch of restful routes like these:

map.resources :users
map.resources :groups

And you have a model that has a polymorphic association, meaning that it has an associated value that can be from more than one model. Here’s an example of how that’s done.
[click to continue…]

{ 2 comments }

A lot of people try Test Driven Development by writing the test, writing the code, and then wondering what the big deal is. Here’s the process I follow along with an explanation of why each step helps.

As a quick note, all tests are written with the RSpec framework.
[click to continue…]

{ 1 comment }

I’ve had a few discussions with people under various circumstances regarding Test First or Test Driven Development (TDD). Some people swear by TDD, while other don’t see how it could possibly work because “I don’t know how my code works until I write it.” My answer to this is “That’s why you need to write the test first.”

My initial exposure to Test Driven Development was while working for a client at SolutionStream. The client insisted that all development be done Test First. At first, it was a painful process. The tests took a while to write, and I really just wanted to get into solving the problem. As time went on, however, I found that writing the tests first, did a few things that made coding much more pleasant.
[click to continue…]

{ 6 comments }

Having used both Test::Unit and RSpec, I have to agree with Jim Weirich: the difference between the two is primarily semantics. It seems to me that functionally, they are both equally capable of verifying and specifying code. However, the way in which you write the tests—the semantics—is the primary difference between the two. That being the case, I prefer the semantics of RSpec.
[click to continue…]

{ 6 comments }

I’ve recently seen several requests come through from people trying to figure out how to call controller methods from their views. The answer is pretty simple. Your controller instance, which is calling your view is stored in the instance variable @controller. So, calling public methods is as simple as this:

@controller.public_method

To call a private method you can do this:

@controller.send("private_method", args)

You should note that if you’re calling controller methods frequently in your views, you should consider putting them into helpers to make them available that way. If you need to manipulate or manage some data, the controller is the place to access the models for this, not the views.

{ 15 comments }