0
0
Ruby on Railsframework~10 mins

belongs_to relationship in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - belongs_to relationship
Define belongs_to in Child Model
Child has foreign key pointing to Parent
Create Child instance with Parent assigned
Access Parent from Child via belongs_to
Use Parent data in Child context
This flow shows how a child model declares a belongs_to link to a parent model, stores the parent's id, and accesses the parent through this link.
Execution Sample
Ruby on Rails
class Comment < ApplicationRecord
  belongs_to :post
end

post = Post.create(title: 'Hello World')
comment = Comment.new(post: post)
comment.post.title
Defines a Comment linked to a Post, creates a post with a title, creates a comment with a post, then accesses the post's title from the comment.
Execution Table
StepActionState ChangeResult
1Define Comment model with belongs_to :postComment model now expects post_id foreign keyChild model linked to Parent
2Create Post instance with title 'Hello World'Post object created with id=1Post saved with id=1
3Create Comment with post: postComment object created with post_id=1Comment linked to Post with id=1
4Access comment.post.titleRails fetches Post with id=1Returns 'Hello World'
5End of exampleNo further state changesExecution stops
💡 All steps completed, comment successfully linked to post and accessed
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
postnilPost(id=1, title='Hello World')Post(id=1, title='Hello World')Post(id=1, title='Hello World')Post(id=1, title='Hello World')
commentnilnilComment(post_id=1)Comment(post_id=1)Comment(post_id=1)
Key Moments - 2 Insights
Why do we need a foreign key like post_id in the Comment model?
The foreign key post_id stores which Post the Comment belongs to. Without it, Rails cannot link the Comment to its Post. See execution_table step 3 where comment gets post_id=1.
How does comment.post.title return the post's title?
belongs_to tells Rails to look up the Post with id=post_id when calling comment.post. Then it accesses the title attribute. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of comment.post_id after step 3?
A1
Bnil
C0
Dpost
💡 Hint
Check the 'State Change' column in step 3 where comment is created with post_id=1.
At which step does Rails fetch the Post record from the database?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Result' columns in step 4 where comment.post.title is accessed.
If the belongs_to line is removed from Comment, what happens when calling comment.post.title?
AReturns the post title anyway
BRaises an error because comment.post is undefined
CReturns nil silently
DCreates a new Post automatically
💡 Hint
belongs_to defines the method comment.post. Without it, the method does not exist.
Concept Snapshot
belongs_to relationship in Rails:
- Declared in child model (e.g., belongs_to :post)
- Child stores foreign key (post_id) linking to parent
- Access parent via child.parent_method (comment.post)
- Rails fetches parent record automatically
- Enables easy navigation from child to parent
Full Transcript
In Rails, the belongs_to relationship connects a child model to a parent model by storing the parent's id in the child. The child model declares belongs_to :parent_name, which tells Rails to expect a foreign key column like parent_name_id. When you create a child instance and assign a parent, Rails saves the parent's id in the child. Later, accessing child.parent_name calls Rails to fetch the parent record using that id. This lets you easily get parent data from the child. For example, a Comment belongs_to a Post. The comment stores post_id. When you call comment.post.title, Rails fetches the Post with that id and returns its title. Without belongs_to, the child cannot link to the parent or access it this way.