Deirdre Saoirse Moen

Sounds Like Weird

Rails and Pluralization

25 August 2005

At first, I didn’t really get why Rails did pluralization of entity names.

One day, I was putting together a list of the relationships in one of my projects so that some of the non-technical people could help sanity check them as I went over them.

class Person < ActiveRecord::Base
has_many :addresses
has_many :phones
has<em>and</em>belongs<em>to_many :conventions, :join</em>table => 'memberships'
end

class Address < ActiveRecord::Base
belongs_to :person
end

class Phone < ActiveRecord::Base
belongs_to :person
end

class Convention < ActiveRecord::Base
has<em>and</em>belongs<em>to_many :persons, :join</em>table => 'memberships'
has<em>many :member</em>type_prices
end

class Membership < ActiveRecord::Base
belongs_to :person
` belongs_to :convention belongsto :attendancetype hasmany :memberpayments end`

…and so on. I think that the pluralization makes the relationships clearer, because the pluralization makes sense in English. When you refer to a single object, it’s singular; when you refer to multiple objects, it’s plural. Makes sense, doesn’t it?

It wasn’t until that moment, though, that I understood the Rails naming conventions, probably because I’d never looked at all the model files at once en masse. While I resisted the convention at first, I genuinely think they make the code clearer.


Related Posts