views:

40

answers:

2

I have a set of columns that applies to more than 1 model.

For the sake of discussion, it could be:

User has many Addresses Company has an Address

These are separate models, but the Address columns will be identical between the 2.

What's the best way to accomplish this in Rails?

+1  A: 

I would just create Address as a separate model.

User and Company would both have relationships with an Address model.

User would use has_many, Company has_one.

The other thing ActiveRecord lets you do is use a composed_of which allows you to collect a set of attributes and compose them into a value object.

Toby Hede
That looks to be what I was searching for. I suppose it doesn't gain me anything special, since I still have to duplicate the columns in my migrations
Ben Scheirman
A: 

I'd have just used a polymorphic Address model personally. Which is what Toby Hede said above, without expressly mentioning polymorphic.

pjammer