muliple relations between two models supported?

16 views
Skip to first unread message

FredO

unread,
Sep 14, 2012, 8:42:27 PM9/14/12
to mon...@googlegroups.com
Is it possible to set up multiple relationships between two models using mongoid?

For example lets suppose that we have model for guides and a model for for tourists. 

Guides have two roles,

1 guide-to-the-egyptian-exhibit
2 guide-to-the-ameican-landscape-art-exhibit

A guide would then have 1 relationship, guide-4-tourists-visiting-the-egyption-exhibit, for one set of tourists,
and another relationship, guide-4-tourists-visitiing-the-american-landscape-art-exhibit, for a second set of tourists.

Can we set up two one-to-many relationships for sguide and tourists?

Crispin Schäffler

unread,
Sep 14, 2012, 9:06:40 PM9/14/12
to mon...@googlegroups.com
yes you can do that. however, in your case it would be better to have a usual many-to-many relationship.

this way you would have

GuideModel
TouristModel
GuideTouristModel

you could also do it like this:

1. GuideModel
2. TourModel (in your example: egyptian-exhibit, american-landscape-art)
3. TouristModel

where
Guide has_many :tours
Tour belongs_to :guide, has_many :tourists
Tourist belongs_to :tour

this way it would be more dynamic since you have your Guides not for one Tour, but a Guide could lead different Tours, so you can add different tours without creating new guides (think of this as a further education for those guides)
Reply all
Reply to author
Forward
0 new messages