0
0
Ruby on Railsframework~10 mins

Why associations connect models in Ruby on Rails - Test Your Understanding

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

Complete the code to declare a one-to-many association in Rails.

Ruby on Rails
class Author < ApplicationRecord
  has_many :[1]
end
Drag options to blanks, or click blank then click option'
Abooks
Bbook
Cauthors
Dpublishers
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular form like 'book' instead of plural 'books'.
Using unrelated model names like 'publishers'.
2fill in blank
medium

Complete the code to declare a belongs_to association in Rails.

Ruby on Rails
class Book < ApplicationRecord
  belongs_to :[1]
end
Drag options to blanks, or click blank then click option'
Abooks
Bpublishers
Cauthor
Dauthors
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural form like 'authors' instead of singular 'author'.
Using unrelated model names like 'publishers'.
3fill in blank
hard

Fix the error in the association declaration to connect models correctly.

Ruby on Rails
class Publisher < ApplicationRecord
  has_one :[1]
end
Drag options to blanks, or click blank then click option'
Apublishers
Bbooks
Cauthors
Dbook
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural form like 'books' with has_one.
Using unrelated model names.
4fill in blank
hard

Fill both blanks to create a has_many through association connecting models.

Ruby on Rails
class Doctor < ApplicationRecord
  has_many :appointments
  has_many :[1], through: :[2]
end
Drag options to blanks, or click blank then click option'
Apatients
Bappointments
Cdoctors
Dhospitals
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated models like 'hospitals'.
Mixing up the order of the association names.
5fill in blank
hard

Fill all three blanks to define a has_many association with dependent destroy option.

Ruby on Rails
class Library < ApplicationRecord
  has_many :[1], dependent: :[2]

  before_destroy :check_[3]
end
Drag options to blanks, or click blank then click option'
Abooks
Bdestroy
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular form for association name.
Using wrong dependent option like 'delete' instead of 'destroy'.
Mismatch between association name and callback method.