Ruby on Rails has_and_belongs_to_many Programmers
Ruby on Rails is really nifty. That's a huge understatement, but it's late and I don't have time to evangelize.
One nifty thing about rails with an un-nifty name is the
has_and_belongs_to_many keyword. It is long and awkward, and it is also
mentally awkward. For example:
class Recipe < ActionRecord::Base
has_many :ingredients
has_and_belongs_to_many :cookbooks
end
Since when did a recipe have a cookbook, or a cookbook belong to a recipe? No,
sorry, this is a terrible name. I appreciate the desire to make it read in a
natural language sort of way, but trying to english-ize complex relations like
this is the sort of thing that gets you in trouble in AI classes and other
places where relations are important. Like, say, relational databases. Anyone
who has the ability to consciously create a many-to-many database table (as
opposed to unconsciously cut-and-pasting one) will know what many_to_many
means, and his brain will happily fill in the gaps to read out "Recipe has a
many-to-many relationship with cookbooks."
I like that I have very few things in Rails to rant about, but I have never been known to hold back my rants. ;-)
Oh, I almost forgot the solution, which was the whole reason I decided to blog it! Thanks to Jamis for figuring out just what the best way to do this is. Append this to the end of config/environment.rb:
class ActionRecord::Base
class << self
alias :habtm :has_and_belongs_to_many
alias :many_to_many :has_and_belongs_to_many
end
end


