This tutorial aims to provide a comprehensive guide on how to use associations in Rails to define relationships between your models. By the end of this tutorial, you'll be able to confidently implement and use different types of model associations in your Rails applications.
Prerequisites:
- Basic knowledge of Ruby and Rails
- Familiarity with databases and SQL
In Rails, an association is a connection between two Active Record models. There are six types of associations:
- Belongs to
- Has one
- Has many
- Has many through
- Has one through
- Has and belongs to many
Let's start with a simple one: "belongs_to" and "has_many".
Let's say we have two models, Author
and Book
. Each book has one author, and each author can have many books.
class Author < ApplicationRecord
has_many :books
end
class Book < ApplicationRecord
belongs_to :author
end
The belongs_to
association sets up a one-to-one connection with another model. The has_many
creates a one-to-many relationship.
has_one
sets up a one-to-one connection but with somewhat different semantics (and consequences). This should be used if the other model contains the foreign key. If the other model cannot exist without this one, the :dependent
option destroys it.
class Supplier < ApplicationRecord
has_one :account
end
class Account < ApplicationRecord
belongs_to :supplier
end
# app/models/author.rb
class Author < ApplicationRecord
has_many :books
end
# app/models/book.rb
class Book < ApplicationRecord
belongs_to :author
end
In the code above, has_many
and belongs_to
create a one-to-many relationship between Author
and Book
. This means that one author can have many books, but each book can have only one author.
# app/models/supplier.rb
class Supplier < ApplicationRecord
has_one :account
end
# app/models/account.rb
class Account < ApplicationRecord
belongs_to :supplier
end
In this example, has_one
and belongs_to
create a one-to-one relationship between Supplier
and Account
. This means that each supplier has only one account, and each account belongs to one supplier.
We have covered the basic Rails associations: belongs_to
, has_many
, and has_one
. You should now understand how to use them to define relationships between your models.
For further learning, explore the other types of associations: has_many :through
, has_one :through
, and has_and_belongs_to_many
.
User
model and a Profile
model with a one-to-one association.Teacher
model and a Course
model with a one-to-many association.Post
model and a Comment
model with a one-to-many association. Each post can have many comments, but each comment belongs to one post.Solutions:
# app/models/user.rb
class User < ApplicationRecord
has_one :profile
end
# app/models/profile.rb
class Profile < ApplicationRecord
belongs_to :user
end
# app/models/teacher.rb
class Teacher < ApplicationRecord
has_many :courses
end
# app/models/course.rb
class Course < ApplicationRecord
belongs_to :teacher
end
# app/models/post.rb
class Post < ApplicationRecord
has_many :comments
end
# app/models/comment.rb
class Comment < ApplicationRecord
belongs_to :post
end
Keep practicing and exploring more complex associations in Rails. Happy coding!