Active Record Associations
Active Record Associations in Rails allows you to establish these relationships and connect models together. When you create an association, you place the foreign key of one model in another to link your models together. This interconnectivity ties back to your database. Your model is linked to your database when you have the model inherit from ActiveRecord::Base.
Through association, Active Record is able to store and retrieve that interconnected data.
Declaring your associations between models will also help streamline revisions and refactoring. It eliminates the pain of parsing through code from multiple models to update a piece of association code.
Here is another way to look at the types of Associations:
- has_many + belongs_to = one-to-many relationship
- has_one + belongs_to = one-to-one relationship
- has_many :through + belongs_to = many-to-many relationship
- has_one :through + belongs_to = one-to-one relationship
- has_and_belongs_to_many = many-to-many relationship
I will focus on the most commonly used associations: belongs_to, has_many and has_many :through. Associations in Rails is declared through a meta-style calling.
Belongs_to Association:
The belongs_to association establishes a one-to-one relationship with another model and thus the association is singular. This association implies that one instance of the declared model belongs to one instance of another model.
Has_many Association:
The has_many association is a one-to-many relationship with another model. This association implies that one instance of the declared model has many of instances of another model, thus the associated model is pluralized. Along with a has_many association, there is usually a belongs_to association with the other model.
Has_many, :through Association:
The has_many, :through association is a many-to-many relationship with another model. This also sets up a join model for the two models associated with the many-to-many connection. It implies that one instance of the declared model has many of instances of another model, through the join model. The join model that is being declared with the has_many association is pluralized within the model that has the declaration and is the through model. For whichever models that the join model belongs_to, it inversely has a has_many association of that join model within those belongs_to models.
The :through join model, allows for a nested has_many association. With a has_many, :through association, there would be two foreign keys. The foreign keys would be of the models on which the has_many associations are declared on and on the table of the join model. For the above example, the join model is Reviews. The Reviews table would have a foreign key of the user (user_id) and trail (trail_id).
Validations for simple model objects
Active Record
includes validation features so data offered to the database can be validated before being accepted and persisted.
Validations are used to ensure that only valid data is saved into your database.
For example, it may be important to your application to ensure that every user provides a valid email address, mailing address and other details.
Model-level validations are the best way to ensure that only valid data is saved into your database. They are database agnostic, cannot be bypassed by end users, and are convenient to test and maintain. Rails make them easy to use, provides built-in helpers for common needs, and allows you to create your own validation methods as well.
I used validations to ensure the user cannot leave a blank review.
ActiveRecord Scope Method
Scope methods add a class method for retrieving and querying objects. These methods are designed to help narrow down all instances of a certain class in your database and can be used in a variety of ways. The syntax is also much cleaner than using a class method.
Devise/OmniAuth
Ruby on Rails application using Devise gem for user authentication. These sign in buttons use a technology called OmniAuth (OAuth for short).
If you are building a small application, you may want to help your users not having to remember an additional password by using only OmniAuth.
The code for the demo app is available on GitHub at https://github.com/monkbroc/omniauth-demo
Nested Resource
The ‘resources’ syntax is a useful way to have Rails generate each of the CRUD routes for you. Take a look below.
Here, in my config/routes.rb file, I am using the ‘resources’ keyword to draw routes for my ‘Trail’ model, I also wanted to expand my model to include ‘/reviews’.
If I want my trail#index action to display a collection of trails that belong to the park that I am navigating under, I will need to pass ‘params[:trails_id]’ in as an argument to stipulate that I only want reviews from the trail who’s ID matches (see below).
And another project under my belt.