views:

330

answers:

1

Hi,

I was modeling a social networking style website where people would be able to have other people on their contacts list, i wanted to model this relationship as a HABTM relation on the Same table i.e User, is this a good idea? or is there a better way to do it?

+4  A: 

It depends. If all you want to do is create relationships between users then you can simply use a HABTM. If you want to store more information about the relationship itself then you may want to make it a separate object. For example, perhaps you want to store what kind of relationship two users have. Friend? Co-worker? Parters?

In that case you could create a separate Relationship or Contact object. Then, User hasMany Contacts and every Contact belongsTo two Users. This is basically the same as manually creating a HABTM relationship, but because the relationship itself is now a separate Model (i.e. Contact) you can store extra information in it.

Sander Marechal
interesting...I will try to do it this way..Thanks
Shiv