What does the has_one relationship represent in Rails?
It means one model owns or is linked to exactly one instance of another model. For example, a User has one Profile.
Click to reveal answer
beginner
How do you declare a has_one association in a Rails model?
Inside the model class, use has_one :associated_model. For example, has_one :profile inside User model.
Click to reveal answer
intermediate
What is the difference between has_one and belongs_to in Rails?
has_one is used in the model that owns the other, while belongs_to is used in the model that holds the foreign key pointing back. They work together to link two models.
Click to reveal answer
intermediate
Where is the foreign key stored in a has_one relationship?
The foreign key is stored in the table of the model that belongs_to the other. For example, if User has_one Profile, the profiles table has a user_id column.
Click to reveal answer
intermediate
How does Rails handle dependent objects in a has_one association?
You can add dependent: :destroy to automatically delete the associated object when the owner is deleted, keeping data clean.
Click to reveal answer
In a Rails has_one association, which model contains the foreign key?
ANeither model has a foreign key
BThe model with <code>has_one</code>
CBoth models have the foreign key
DThe model with <code>belongs_to</code>
✗ Incorrect
The foreign key is stored in the model that uses belongs_to, pointing back to the owner.
Which method declaration correctly sets up a has_one association in Rails?
Ahas_one :profile
Bbelongs_to :profile
Chas_many :profile
Dhas_one_of :profile
✗ Incorrect
Use has_one :profile to declare a one-to-one association where the model owns one profile.
What happens if you add dependent: :destroy to a has_one association?
ANothing happens automatically
BThe associated object is deleted when the owner is deleted
CThe owner is deleted when the associated object is deleted
DBoth objects are deleted simultaneously
✗ Incorrect
Adding dependent: :destroy deletes the associated object automatically when the owner is removed.
If a User model has has_one :profile, how do you access the profile of a user instance?
Auser.profile
Buser.profiles
Cprofile.user
Dprofile.users
✗ Incorrect
You call user.profile to get the single profile linked to that user.
Which of these best describes the has_one relationship?
AModels are unrelated
BOne model owns many instances of another model
COne model owns exactly one instance of another model
DMany models belong to one model
✗ Incorrect
has_one means one model owns exactly one instance of another model.
Explain how the has_one relationship works in Rails and where the foreign key is stored.
Think about which model points to the other with an ID.
You got /4 concepts.
Describe how to set up a has_one association with dependent destruction in Rails.
Consider what happens when you delete the main object.
You got /3 concepts.
Practice
(1/5)
1. What does the has_one association in Rails represent?
easy
A. A many-to-many connection between two models
B. A one-to-many connection from one model to many others
C. A one-to-one connection from one model to another
D. A model inheriting from another model
Solution
Step 1: Understand the purpose of has_one
The has_one association sets up a one-to-one link from one model to another, meaning each instance of the first model is connected to exactly one instance of the second model.
Step 2: Compare with other associations
Unlike has_many (one-to-many) or has_and_belongs_to_many (many-to-many), has_one is strictly one-to-one.
Final Answer:
A one-to-one connection from one model to another -> Option C
Quick Check:
has_one = one-to-one [OK]
Hint: Remember: has_one means exactly one related record [OK]
Common Mistakes:
Confusing has_one with has_many
Thinking has_one creates many related records
Mixing up has_one with belongs_to
2. Which of the following is the correct syntax to declare a has_one association in a Rails model?
easy
A. has_one = :profile
B. has_one profile
C. has_one => :profile
D. has_one :profile
Solution
Step 1: Recall Rails association syntax
In Rails, associations are declared using the method name followed by a symbol representing the related model, e.g., has_one :profile.
Step 2: Identify correct Ruby syntax
has_one :profile uses the correct Ruby method call with a symbol argument. Options A, B, and C are invalid Ruby syntax for this purpose.
Final Answer:
has_one :profile -> Option D
Quick Check:
Association syntax uses symbol with colon [OK]
Hint: Use colon before association name: has_one :name [OK]
Common Mistakes:
Omitting the colon before the association name
Using hash rocket syntax incorrectly
Assigning association with equals sign
3. Given these models:
class User < ApplicationRecord
has_one :profile
end
class Profile < ApplicationRecord
belongs_to :user
end
What will user.profile return if the user has no profile record?
medium
A. nil
B. An error is raised
C. An empty Profile object
D. A new unsaved Profile instance
Solution
Step 1: Understand has_one association behavior
If no associated record exists, calling user.profile returns nil because there is no linked profile.
Step 2: Confirm no automatic creation
Rails does not create or return empty or new instances automatically for has_one associations.
Final Answer:
nil -> Option A
Quick Check:
No associated record means nil [OK]
Hint: No linked record? Association returns nil [OK]
Common Mistakes:
Expecting an empty or new object instead of nil
Assuming an error is raised when missing association
Confusing with build_profile method
4. Identify the error in this code snippet:
class User < ApplicationRecord
has_one :profile
end
class Profile < ApplicationRecord
has_one :user
end
medium
A. Profile should use belongs_to :user instead of has_one
B. User should use belongs_to :profile instead of has_one
C. Both models should use has_one for the association
D. No error; the code is correct
Solution
Step 1: Understand correct association directions
In a one-to-one relationship, the model holding the foreign key uses belongs_to. Here, Profile should belong to User.
Step 2: Identify incorrect association
Using has_one :user in Profile is wrong because it implies Profile owns User, which is not the case.
Final Answer:
Profile should use belongs_to :user instead of has_one -> Option A
Quick Check:
Foreign key side uses belongs_to [OK]
Hint: belongs_to goes on model with foreign key [OK]
Common Mistakes:
Using has_one on both sides
Confusing which model holds foreign key
Omitting belongs_to in related model
5. You want to ensure that when a User is deleted, their associated Profile is also deleted automatically. How do you modify the has_one association in the User model?
hard
A. has_one :profile, autosave: true
B. has_one :profile, dependent: :destroy
C. has_one :profile, validate: true
D. has_one :profile, inverse_of: :user
Solution
Step 1: Understand dependent option in associations
The dependent: :destroy option tells Rails to delete the associated record when the owner is deleted.
Step 2: Apply to has_one association
Adding dependent: :destroy to has_one :profile ensures the profile is removed automatically with the user.
Final Answer:
has_one :profile, dependent: :destroy -> Option B
Quick Check:
dependent: :destroy deletes associated record [OK]
Hint: Use dependent: :destroy to auto-delete associated record [OK]
Common Mistakes:
Using autosave or validate instead of dependent
Forgetting to add dependent option
Confusing inverse_of with dependent
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.