Share This Article
Rails associations are a way to link one model to another. There are four different types of associations:
- One-to-one
- One-to-many
- Many-to-many
- Polymorphic
Each type of association has its own set of methods that you can use to interact with the associated data.
One-to-One Associations
A one-to-one association means that each record in one model is associated with one record in another model.
For example, you could have a one-to-one association between a user and a profile.
To set up a one-to-one association, you must add a belongs_to association to both models.
# app/models/user.rb
class User < ActiveRecord::Base
belongs_to :profile
end
# app/models/profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
You can then access the associated data using the association name.
user = User.first
user.profile
# or
profile = Profile.first
profile.user
If you want to create a new record for the association, you can use the association name with an exclamation mark.
user = User.first
user.profile!
# or
profile = Profile.first
profile.user!
One-to-Many Associations
A one-to-many association means that each record in one model is associated with many records in another model.
For example, you could have a one-to-many association between a user and their posts.
To set up a one-to-many association, you need to add a has_many association to the model that has many records.
# app/models/user.rb
class User < ActiveRecord::Base
has_many :posts
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
end
You can then access the associated data using the association name.
user = User.first
user.posts
# or
post = Post.first
post.user
If you want to create a new record for the association, you can use the association name with an exclamation mark.
user = User.first
user.posts.create!
# or
post = Post.first
post.create_user!
Many-to-Many Associations
A many-to-many association means that each record in one model is associated with many records in another model and vice versa.
For example, you could have a many-to-many association between posts and tags.
To set up a many-to-many association, you need to add a has_many :through association to both models.
# app/models/post.rb
class Post < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
end
# app/models/tag.rb
class Tag < ActiveRecord::Base
has_many :taggings
has_many :posts, through: :taggings
end
# app/models/tagging.rb
class Tagging < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
You can then access the associated data using the association name.
post = Post.first
post.tags
# or
tag = Tag.first
tag.posts
If you want to create a new record for the association, you can use the association name with an exclamation mark.
post = Post.first
post.tags.create!
# or
tag = Tag.first
tag.posts.create!
Polymorphic Associations
A polymorphic association means that a model can be associated with more than one other model.
For example, you could have a polymorphic association between comments and commentable models.
To set up a polymorphic association, you need to add a belongs_to association with a :polymorphic option to the model that will be associated with more than one model.
# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
You can then access the associated data using the association name.
comment = Comment.first
comment.commentable
# or
comment = Comment.first
comment.commentable_type
comment = Comment.first
comment.commentable_id
If you want to create a new record for the association, you can use the association name with an exclamation mark.
comment = Comment.first
comment.commentable!
# or
comment = Comment.first
comment.commentable_type!
comment = Comment.first
comment.commentable_id!
Summary
In this article, you’ve learned about the different types of associations and how to set them up. You’ve also learned about some methods you can use to interact with the associated data.