Recall & Review
beginner
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?✗ 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?✗ 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?✗ 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?✗ Incorrect
You call
user.profile to get the single profile linked to that user.Which of these best describes the
has_one relationship?✗ 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.