whirlwin
whirlwin

Reputation: 16521

Ruby on Rails relations

This is homework, just so that's mentioned.

I have project called AdventCalendar, which is supposed to be an Advent Calendar, (obviously). I have made it possible to log in/ log out, or create new users. Once you have logged in you may create Calendars (Advent calendars). Each calendar has many days, and a day belongs to one calendar. Also, a user can create many calendars.

So, what I want is to make it possible for a user to share their calendars with other users if they want to. I'm kind of stuck and don't know where to go from here. I thought maybe to add a

has_and_belongs_to_many :users

to calendar, and likewise:

has_and_belongs_to_many :calendars

to users..

I just don't know how to implement it. Any tips, or help is highly appreciated!

Thanks in advance.

Upvotes: 0

Views: 121

Answers (1)

Trip
Trip

Reputation: 27114

Yes you're right on point here. Though I think its a little less complicated. Calendars do not have to have many users. The relationship, hierarchically is ok with just users having calendars. Then, for calendars, create a polymorphic join table to allow a calendar to have many users associated with.

#user.rb

 has_many    :calendars


#calendar.rb

 belongs_to  :many_users, :polymorphic => true
 has_many    :users, :as => :many_users

Your calendar table should have a many_users_id, and a user_id field.

Upvotes: 2

Related Questions