Archive for the ‘Rails’ Category

No RailsConf for me

Sunday, August 20th, 2006

Too bad. A new project popped up on the horizon, and its most important deadline is September 15th. The choice was either the project (and therefore earning money) or RailsConf Europe in London.

Well, see you next year, or at RailsConf Germany.

Fun with Fixtures

Thursday, May 18th, 2006

Fixtures are a neat feature of Rails. However there’s a small caveat that pops up when your classes don’t follow the conventions and use a table whose name is not a pluralized version of the class name, e.g. the class is `HorseShoe` and the table name is just `shoes`. The corresponding fixture would have to be name shoes.yml and included in the test case with `fixtures :shoes`. Now as soon as you try to access one of the fixtures through `shoes(:iron_shoe)`, the fixtures code will try to look up the class name to instantiate it. With the default conventions it will look for `Shoe`. To avoid this, I found one way (probably not the only one) to tell the fixtures code what class to use. After defining your fixtures you can tell it which class to use for which fixture:

class ShoeTest < Test::Unit::TestCase
fixtures :shoes
set_fixture_class :shoes => “HorseShoe”

end

The call to `set_fixture_class` will merge your custom table-class mapping into its own, and whenever you’re trying to access one of the fixtures, it will default to your configured class instead of the singularized table name.

As I said, this probably isn’t the only way, but it is one way I found while browsing through the fixtures code.