0
0
Ruby on Railsframework~20 mins

has_one relationship in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HasOne Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this has_one association output?
Given the following Rails models, what will user.profile.name output if the profile exists?
Ruby on Rails
class User < ApplicationRecord
  has_one :profile
end

class Profile < ApplicationRecord
  belongs_to :user
  # attribute :name, :string
end

user = User.create(name: 'Alice')
user.create_profile(name: 'Alice Profile')

puts user.profile.name
AAlice Profile
Bnil
CRaises NoMethodError
DRaises ActiveRecord::RecordNotFound
Attempts:
2 left
💡 Hint
Remember that has_one creates a method to access the associated object.
state_output
intermediate
2:00remaining
What is the value of profile.user after creation?
Given these models and code, what will profile.user.name output?
Ruby on Rails
class User < ApplicationRecord
  has_one :profile
end

class Profile < ApplicationRecord
  belongs_to :user
end

user = User.create(name: 'Bob')
profile = user.create_profile

puts profile.user.name
ARaises NoMethodError
Bnil
CBob
DRaises ActiveRecord::RecordInvalid
Attempts:
2 left
💡 Hint
Check the belongs_to association from Profile to User.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a has_one association with dependent destroy?
You want to define a has_one association where deleting the parent deletes the child. Which code is correct?
Ahas_one :profile, dependent: destroy
Bhas_one :profile, dependent: :destroy
Chas_one :profile, dependent => :destroy
Dhas_one :profile, dependent: 'destroy'
Attempts:
2 left
💡 Hint
Check the syntax for passing options as symbols.
🔧 Debug
advanced
2:00remaining
Why does user.profile return nil unexpectedly?
Given these models and code, why does user.profile return nil even after creating a profile?
Ruby on Rails
class User < ApplicationRecord
  has_one :profile
end

class Profile < ApplicationRecord
  belongs_to :user
end

user = User.create(name: 'Carol')
Profile.create(name: 'Carol Profile')

puts user.profile
Ahas_one association requires reload to update, so user.profile is stale
BUser must use has_many for multiple profiles, so has_one returns nil
Cbelongs_to is missing in Profile, so association fails
DProfile was created without linking to the user, so user.profile is nil
Attempts:
2 left
💡 Hint
Check if the profile is linked to the user when created.
🧠 Conceptual
expert
3:00remaining
What happens if you call build_profile twice on the same user?
Consider a user with no profile. You call user.build_profile(name: 'First') then user.build_profile(name: 'Second') without saving. What is the state of user.profile.name?
Ruby on Rails
class User < ApplicationRecord
  has_one :profile
end

class Profile < ApplicationRecord
  belongs_to :user
end

user = User.create(name: 'Dave')
user.build_profile(name: 'First')
user.build_profile(name: 'Second')

puts user.profile.name
ASecond
BFirst
Cnil
DRaises ActiveRecord::RecordNotSaved
Attempts:
2 left
💡 Hint
Think about what build_profile does and if it replaces the previous unsaved profile.