This flow shows how a has_one relationship links two models, allowing one model to own exactly one instance of another.
Execution Sample
Ruby on Rails
class User < ApplicationRecord
has_one :profile
end
class Profile < ApplicationRecord
belongs_to :user
end
user = User.create(name: "Anna")
profile = Profile.create(user: user, bio: "Loves cats")
user.profile.bio = "Loves dogs"
user.profile.save
Defines models with has_one and belongs_to, creates linked instances, accesses and modifies via association.
Execution Table
Step
Action
Model Instances
Association State
Output/Result
1
Define has_one :profile in User model
User model, Profile model
No instances yet
Ready to link User to one Profile
2
Create User instance user
User(id:1, name:"Anna")
user.profile is nil
User created without profile
3
Create Profile instance linked to user
Profile(id:1, user_id:1, bio:"Loves cats")
user.profile points to Profile(id:1)
Profile linked to user
4
Access user.profile
User(id:1), Profile(id:1)
Association intact
Returns Profile instance with bio "Loves cats"
5
Modify profile via user.profile
Profile(id:1, user_id:1, bio:"Loves dogs")
Association unchanged
Profile bio updated through user.profile
6
End of example
User and Profile exist
Association stable
user.profile returns updated Profile instance
💡 All steps complete, association established, accessible, and modifiable
Variable Tracker
Variable
Start
After Step 2
After Step 3
After Step 4
After Step 5
Final
user
nil
User(id:1, name:"Anna")
User(id:1, name:"Anna")
User(id:1, name:"Anna")
User(id:1, name:"Anna")
User(id:1, name:"Anna")
profile
nil
nil
Profile(id:1, user_id:1, bio:"Loves cats")
Profile(id:1, user_id:1, bio:"Loves cats")
Profile(id:1, user_id:1, bio:"Loves dogs")
Profile(id:1, user_id:1, bio:"Loves dogs")
user.profile
nil
nil
Profile(id:1, user_id:1, bio:"Loves cats")
Profile(id:1, user_id:1, bio:"Loves cats")
Profile(id:1, user_id:1, bio:"Loves dogs")
Profile(id:1, user_id:1, bio:"Loves dogs")
Key Moments - 4 Insights
Why does user.profile return nil before the profile is created?
Because the association is empty until a Profile instance with matching user_id is created, as shown in step 2 and 3 of the execution_table.
Can a user have more than one profile with has_one?
No, has_one enforces one associated object per owner; additional profiles with the same user_id will have the first one (by ID) returned typically, but use has_many for multiple.
How does Rails know which profile belongs to which user?
By convention, Rails looks for user_id foreign key in the Profile table matching the user's id, as seen in the Profile instance data.
What happens if I assign user.profile = another_profile?
It updates the association: the new profile gets user_id set, old one loses it (if using dependent options or manually), ensuring only one.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of user.profile after step 2?
AProfile instance
Bnil
CError
DUser instance
💡 Hint
Check the 'Association State' column at step 2 in execution_table
At which step does user.profile first return a Profile instance?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Association State' and 'Output/Result' columns in execution_table
After step 5, what is the bio of user.profile?
A"Loves cats"
Bnil
C"Loves dogs"
DError
💡 Hint
See the modification in step 5 and final state in execution_table
Concept Snapshot
Rails has_one association:
- Owner model: has_one :associated
- Associated model: belongs_to :owner
- Owner accesses via owner.associated (singular)
- FK: owner_id in associated table
- Returns nil if no match, first match if multiples
- Supports assignment, autosave, dependent options
Full Transcript
This VMC trace demonstrates Rails has_one association between User and Profile. Models are defined with has_one and belongs_to. User created alone has nil profile. Linking Profile via user: sets foreign key. Access/modify via user.profile works seamlessly. Tables track state evolution, variables, key behaviors like nil until linked, single association enforcement. Quizzes reinforce timing and effects.