Course: Course List » Ruby on Rails » Advanced Forms and Active Record » Project: Associations
##Objective:
…build a site similar to a private Eventbrite which allows users to create events and then manage user signups.
There’s countless ways to customize and implement this project, so rather than doing a step-by-step walkthrough, I decided to deploy a demo and describe how I set up the models and associations.
I used twitter bootstrap to work on CSS styling. I saved the extra credit (sending and accepting invites to/from users) for a later date.
The primary focus of this project is to build associations between a User and Events models as well as a ‘through’ model I named Invites. I plan to discuss each model; below is a snapshot of the models and associations at the end of the project:
##User Model
A user can create many events and attend many events, so there are two types of has_many relationships with the Event model: user being a creator and/or user attending an event.
We create a creator_id field in the Event model, so that each event has one creator (user). We could’ve created a user_id field in the Event model, but in this case, we’re more explicit by naming it creator_id.
The creator_id is a foreign key, which links to the user_id from the User model.
When a user attends an event, we use an association through the Invite model. This is because an event can have many attendees, which is different than having only one creator.
The methods you see in the User model class are used to query data and build/destroy associations. They are used in the views and should make more sense after building out the Event and Invite models.
##Event Model
In the User model, we created the has_many relationship between the User and Event models. Now, we build the other side of the relationship where an Eventbelongs_to a creator (User). Rails knows to look for creator_id field when you include the line below:
An Event has many Users (attendees), through invites. Note that the Invite model allows us to have two has_many relationships: a user has many events, and an event has many users.
We use scopes to query for upcoming and past events in our application. We use these scopes in some of the methods defined in the User model, to pull a specific user’s upcoming and past events.
##Invite Model
The Invite model brings the User and Event models together, where a user can have (attend) many events, and events can have many users (attendees). This puts our associations all together. Finally, we can verify that the assocations are working correctly.
Verify Associations
From the command line, you should be able retrieve events a user has created as well as build new events. Note that I have already populated data.
You should be able to retrieve invites as well as attended_events from the command line:
Note, the invites do not give you much information. Rather, you really want the information from attended_events (through the invites association).
Check upcoming and past scopes:
And lastly verify that you can get event’s creator and attendees:
At this point, we have built the models and associations. The rest of the project is building the user interface.