Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of join table in Rails?

For what purposes I'd potentially need to use join table?

For example, when I run rails g migration CreateJoinTable products suppliers, it creates the respective products_suppliers table with products_id and suppliers_id columns. Moreover, these fields have the option :null set to false by default. Why are those fields needed? What they are used for? And what is that null option?

Thanks in advance.

like image 411
Billy Logan Avatar asked Jan 02 '16 11:01

Billy Logan


People also ask

What is a join table?

Back to join tables. A join table is a data table that has multiple outgoing connections - connecting multiple data tables to one data table. This will make sense in a minute, hang on. Here's our real life example. We have an app to manage Event scheduling. We could build this with just two data tables.

How do I migrate a join table from one database to another?

Run the migration: rake db:migrate. As a result, the join table should now exist in your database. You are now able to use the methods generated by the methods belongs_to and has_many:

When to use includes in rails?

For example, we’d want to use :includes if we were to load all active employees at companies that are verified and then print each employee’s name and their company name. For more information on :includes, you can refer to A Visual Guide to Using :includes in Rails.

How many columns does a join model have?

A join model, however, will have two columns: student_id and class_id. (Incidentally, calling a model 'Class' in Ruby is asking for trouble. Might I suggest 'Course'?)


1 Answers

It's standard relational database functionality.

--

Rails is designed on top of a relational database (typically MYSQL or PGSQL), which basically means that you're able to reference "associated" data through the use of foreign keys:

enter image description here

In context of relational databases, a foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table


In the case of Rails, "relationships" in the database are maintained by ActiveRecord - an ORM (Object Relational Mapper). This means that from the application layer, you just have to focus on populating objects:

@user = User.find x
@user.products #-> outputs records from "products" table with foreign key "user_id = x"

ActiveRecord manages your associations, which is why you have to define the belongs_to / has_many directives in your models etc.

Most associations you create will be reference other tables directly:

#app/models/user.rb
class User < ActiveRecord::Base
   has_many :products
end

#app/models/product.rb
class Product < ActiveRecord::Base
   belongs_to :user
end

enter image description here

The problem with this is that it only allows you to associate single records; if you wanted to associate multiple (many-to-many), you need a join table.


Rails uses join tables for has_many :through and has_and_belongs_to_many relationships...

enter image description here

Join tables are populated with (at least) the primary key & foreign key of a relationship. For example...

user_id | product_id
   1    |     3
   1    |     5
   2    |     3

This allows you to call:

#app/models/user.rb
class User < ActiveRecord::Base
   has_and_belongs_to_many :products
end

#app/models/product.rb
class Product < ActiveRecord::Base
   has_and_belongs_to_many :users
end

@user.products #-> all products from join table
@product.users #-> all users from join table

In short, if you want to have has_many <-> has_many associations, the join table is necessary to store all the references to the relative foreign keys.

like image 143
Richard Peck Avatar answered Sep 19 '22 13:09

Richard Peck