Saturday, September 20, 2008

ActiveRecord Goodies


There are 3 things in the goody basket tonight:
  1. Multiple belongs_to and has_many Associations: Say you have a Game object. It makes sense to have two instances of a Player object associated with it. One player is the winner, one is the loser. Do you know how to model that relationship in active record? Here is a fantastic blog post that gives a nice explanation on how to do it. If you want another example take a look at my code which does the same thing.
  2. Logging the sql ActiveRecord generates: In rails it is obvious how to make this happen. If you are using ActiveRecord standalone you can do this:
    ActiveRecord::Base.logger = Logger.new(STDOUT)
  3. Deciphering how ActiveRecord pluralizes: Sometimes I have trouble figuring out what ActiveRecord will compute the plural of a noun to be. This application shows you what ActiveRecord is thinking.

No comments: