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.
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.
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:
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.
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'?)
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:
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
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With