Mikey Hogarth

Digital Media Development, Code and Opinions

Basic ActiveRecord Associations Overview

Introduction

This is just a brief overview of the active record association methods that are available in the RoR default environment.

One-to-One relation

Tabular representation:

Table 1’s primary key exists as a foreign key in Table 2.

ActiveRecord representation:

Model1 has_one :model2
Model2 belongs_to :model1

Where model 1 is “Table 1” and model 2 is “Table 2”.

Allows code such as;

Assuming Countries and Flags were tables in your database;

Country.first.flag	#would return a single flag instance
Flags.first.country	#would return a single country instance

One-to-Many relations

Tabular representation:

Table 1’s primary key exists as a foreign key in Table 2.

ActiveRecord representation

Model1 has_many :model2s
Model2 belongs_to :model1

Where model 1 is “Table 1” and so on.

Allows code such as;

Assuming Countries and Cities were tables in your database;

Country.first.cities  	#would return an array of City instances
City.first.country	#would return a single country instance

Many-to-Many relations: Link table method

Tabular representation:

Table 1 and table 2 both have a primary key, and a third “link table” exists containing columns representing the primary keys of the other two tables. This is the only reason that the link table exists, and as such these are the only two columns it has. It in itself is not a model.

ActiveRecord representation:

Model1 has_and_belongs_to_many :model2s
Model2 has_and_belongs_to_many :model1s

Where model 1 is “Table 1” and so on. You’d also need to have generated the migration that would create the link table.

Allows code such as;

Assuming Countries and Languages were tables in your database;

Country.first.languages  	#would return an array of Language instances
Language.first.countries 	#would return an array of Country instances

Many-to-Many relations: Model method

Example Tabular representation:

Table 1, table 2 and table 3 all have a primary keys. All three tables represent models. Table 1 contains Table 2’s Primary Key as a foreign key, and table 2 contains table 3’s primary key as a foreign key. All three tables represent models.

ActiveRecord representation:

The above table representation essentially means the following;

  • Table 1 has a many-to-one relationship with table 2
  • Table 2 has a many-to-one relationship with table 3

so models would look like this;

Model1 belongs_to :model2
Model2 has_many :model1s
Model2 belongs_to :model3
Model3 has_many :model2s

Where object 1 is “Table 1” and so on.

You can see by the relations that because model 2 has many model 1s, and model 3 has many model 2s, therefore the following is also true;

  • Model 3 has many model 1s (because it is vicariously joined through model 2).

You can represent this in Active record as follows;

Model3 has_many :model1s :through => :model2s

Allows code such as;

Assuming Districts, Cities and Countries were tables in your database, and that they are table/model 1, 2 and 3 in that order;

Country.first.cities			#would return an array of city instances
City.first.country			#would return a single country instance
City.first.districts			#would return an array of district instances
District.first.city			#would return a single city instance

use of the :through symbol allows you to do this;

Country.first.districts		  	#would return an array of district instances
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 469 other followers

%d bloggers like this: