Well, we’re back on track! The join tables are back, the invites work, the friendship model works and all the tests are green!
It was a little difficult to get the invites model working again, mostly because mongoid does not have an equivalent of the Has_many_through relationship. You see, you can have an invites table that references both an event and a user, but if you want to get all the events related to a user, you can’t do that because mongoid does not support Join queries. However, you can query all the invites related to the user, and map all the eventids in those invites to a query on the events collection. So far, it seems to be emulating the has_many through relationship just fine.
Today we are going to do something a little differently with the Events model. Before I had the events belong to the users model. I had an issue when I wanted to query all of the Events a user had created and had been invited to. It had to be a join between all of the events that the User had created and all of the events related to the invites the user had been associated with. That seemed to be a sign of trouble for me, because again, Mongoid doesn’t do join queries.
So I figured, since I have the invites join table that I can add attributes to, why not set an invite’s status to organizer? That way I can get all the associated events from a user in a single query. If an event’s organizer drops out, the organizer status could revert to somebody else in the event.
But how do we create a record with a nested record already in there? Better yet, how do we test all this? I’m going to start off by using a hidden field in the Events form, and putting a :create validation in the Events Model. Once that gets done, I hope to get started on the mailers.