0
votes

In a new rails app, I have to build this sort of relationship

Trip -> 1-to-1 -> Plan
Plan -> n-to-n -> Places
Plan -> n-to-n -> Activities

and ofcourse the vice-versa relationship as well..

The plan table is something I'm thinking about. Whether it is possible to have a direct Trip -> Places and Trip -> Activities relationship without any extra table.

If not, then what could be and efficient way to handle this kind of relationship, keeping in mind that there might be other models which could have a n-to-n relationship with the Plan model in the future.

UPDATE - I found this question and the answer to work. Might be helpful for someone looking to do the same as I am

ActiveRecord, has_many :through, and Polymorphic Associations

Thank you

1

1 Answers

3
votes

It's possible, but complicated and messy, so I wouldn't. You can define has_many ..., :through => :plan associations to easily and efficiently access the places and activities for trips, etc.

Rails doesn't offer the :through option for belong_to associations, but you can define convenience methods to access those, and you can query the associated records efficiently by using the :include option when finding.