The Ruby language has a library called ActiveRecord which provides object relational mapping (ORM) to its developers. ActiveRecord provides a TON of helpful tools for developers to help cut down on the hassle of rewriting the same old code that is necessary for any application to get up and running. One of the helpful tools that ActiveRecord provides Ruby developers is Associations. Now don't get me wrong, these associations can get a little confusing at times but are incredibly helpful when building an application that includes more that one model.
To start, Associations are simply a way of connecting two or more models in a database together. When associating models together we are allowing one (or both) to know to know about the other. When models know about each other we open up a whole new ball game! I utilized these associations through out my application, FitPlanningPartner, which I will walk you through.
belongs_to
The belongs to association on a model informs it that they are essentially a child to the model linked to the end of the belongs to statement. With this relationship, the model will have knowledge that it is associated with its parent. The way this table identifies with its parents in the database is by having a foreign key attribute that connects with the parents primary key. For instance, in my application a Workout belongs_to a User so in my database, my Workout table would have a column for user_id. Now to associate the two in our Workout model with ActiveRecord, we simply connect the two by listing belongs_to :User
to create that association.
has_many
Now, on the other end of a belongs to is the has many side of the relationship. The model that holds the has many can have multiple instance of the other model. Our parent does not necessarily need to know about its child, so in the database there is no specific link to its child. In our model we simply state that is has many of the child model. In my application my User class would have many workouts. This would look something like: has_many :Workouts
. This is listed as plural because our model could have multiple.
has_many, through
This is were it gets really interesting. A has many, through relationship allows a set up for a many to many association also referred to as a join table. This can sound confusing so I will provide an example based upon my application. I created a table, Routines, that joined my Workout and Exercise table. This would mean that my routine instance could then link together my Workout and Exercise through itself. In other words, Workouts have many Exercises through Routines and Exercises have many Workouts through Routines. Having this association allowed me to view Exercises associated with a Workout or what Exercises were being used in Workouts. Thanks to the through part of the relationship I was able to do
@exercise.workouts.count
and get a count of how many workouts included a specific exercise.
These are just a few of the many associations ActiveRecord helps developers with! I hope this post provided some insight into the wonderful world of associations.
Resources:
Top comments (0)