0
0
Ruby on Railsframework~10 mins

has_one relationship in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a has_one association in a Rails model.

Ruby on Rails
class User < ApplicationRecord
  [1] :profile
end
Drag options to blanks, or click blank then click option'
Abelongs_to
Bhas_one
Chas_many
Dhas_and_belongs_to_many
Attempts:
3 left
💡 Hint
Common Mistakes
Using belongs_to instead of has_one here.
Using has_many which is for multiple related objects.
2fill in blank
medium

Complete the code to declare the inverse belongs_to association in the related model.

Ruby on Rails
class Profile < ApplicationRecord
  [1] :user
end
Drag options to blanks, or click blank then click option'
Ahas_one
Bhas_many
Cbelongs_to
Dhas_and_belongs_to_many
Attempts:
3 left
💡 Hint
Common Mistakes
Using has_one here instead of belongs_to.
Using has_many which is for multiple related objects.
3fill in blank
hard

Fix the error in the has_one declaration to specify the class name correctly.

Ruby on Rails
class User < ApplicationRecord
  has_one :[1], class_name: 'UserProfile'
end
Drag options to blanks, or click blank then click option'
Auserprofiles
Bprofiles
Cuser_profile
Dprofile
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural form like 'profiles' for has_one.
Using underscored or concatenated names that don't match the model.
4fill in blank
hard

Fill both blanks to complete the has_one association with dependent destroy option.

Ruby on Rails
class User < ApplicationRecord
  [1] :profile, [2]: :destroy
end
Drag options to blanks, or click blank then click option'
Ahas_one
Bdependent
Cbelongs_to
Doptional
Attempts:
3 left
💡 Hint
Common Mistakes
Using belongs_to instead of has_one for the association.
Using optional instead of dependent for the option.
5fill in blank
hard

Fill all three blanks to complete a has_one association with class name and foreign key options.

Ruby on Rails
class User < ApplicationRecord
  [1] :profile, class_name: '[2]', foreign_key: '[3]'
end
Drag options to blanks, or click blank then click option'
Ahas_one
BUserProfile
Cuser_id
Dbelongs_to
Attempts:
3 left
💡 Hint
Common Mistakes
Using belongs_to instead of has_one for the association.
Incorrect class name spelling or casing.
Wrong foreign key name that doesn't match the database column.