Building database relationship: a product compatible with another product

60 views Asked by At

I'm building a database with Ruby on Rails which basically store products. One of my main concern is that in the future, I want to be able to know the compatibility which two products.

I don't know how to build that in "a Rails way". First I was thinking about creating a new table Compatibility with the two product_id but I guess there is one more elegant solution ?

1

There are 1 answers

2
Arslan Ali On BEST ANSWER

Self-Referential Association is the solution. A product is compatible with another product, so the relationship is between two same items.

Second thing, this is the inverse relationship as well. If a product A is compatible with product B, then product B is also compatible with product A. The railscasts mentioned in the link clearly explains this thing.