I have several data models that don’t directly know about each other, but need to be connected somehow so results of the opposite type can be returned when searching against the first. But which method to use?
- make a reference table of only foreign keys – no other attributes(? used to but deprecated)
- no extra models – Just link to the opposite class from the both models.
- connected via a table
- can only join two models
- make a table with your two ids, and other attributes (editable?)
- make a (join?) model for that table, that lets it know it belongs to the two models you are linking
- tell each model that it is linked to it’s opposite model :through=> the joined model.
- connected via a model
- can spiderweb multiple models together
Either will work for most things. has_many :through seems to be more versatile (if a little more work required to get back what you want), especially if you want extra attributes. habtm is quick, and simple, but not quite as diverse in it’s functionality.
Now looking closer at what I need to do, I will probably be required to connect at least three things together – and who knows, maybe more! Which would make has_many :through the way to go.
Good articles for more information or examples:
<input ... >
Originally published at thisOrange.com. You can comment here or there.