0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Associations Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use associations between models in Rails?
What is the main reason Rails uses associations to connect models?
ATo enforce strict data types on model attributes
BTo easily retrieve related data without writing complex SQL queries
CTo automatically generate user interface elements
DTo speed up the Rails server startup time
Attempts:
2 left
💡 Hint
Think about how Rails helps you get related records.
component_behavior
intermediate
2:00remaining
What happens when you call has_many in a model?
In Rails, what does adding has_many :comments inside a Post model do?
AGenerates a new database table for comments
BAutomatically deletes all comments when the post is created
CPrevents comments from being saved without a post
DCreates a method to fetch all comments related to that post
Attempts:
2 left
💡 Hint
Think about what has_many means in terms of data access.
state_output
advanced
2:00remaining
Output of accessing associated records
Given these models:
class Author < ApplicationRecord
  has_many :books
end

class Book < ApplicationRecord
  belongs_to :author
end

author = Author.create(name: 'Jane')
author.books.create(title: 'Book 1')
author.books.create(title: 'Book 2')

puts author.books.count

What is the output?
A2
B1
C0
DRaises an error
Attempts:
2 left
💡 Hint
How many books did we add to the author?
📝 Syntax
advanced
2:00remaining
Identify the correct association syntax
Which option correctly sets up a one-to-one association where a User has one Profile?
A
class User &lt; ApplicationRecord
  has_one :profile
end

class Profile &lt; ApplicationRecord
  belongs_to :user
end
B
class User &lt; ApplicationRecord
  belongs_to :profile
end

class Profile &lt; ApplicationRecord
  has_one :user
end
C
class User &lt; ApplicationRecord
  has_many :profile
end

class Profile &lt; ApplicationRecord
  belongs_to :user
end
D
class User &lt; ApplicationRecord
  has_one :profiles
end

class Profile &lt; ApplicationRecord
  belongs_to :user
end
Attempts:
2 left
💡 Hint
Remember singular vs plural and which side owns the foreign key.
🔧 Debug
expert
3:00remaining
Why does accessing an association raise an error?
Given these models:
class Order < ApplicationRecord
  has_many :items
end

class Item < ApplicationRecord
  belongs_to :order
end

order = Order.new
order.items.first.name

Why does this code raise an error?
ABecause <code>belongs_to</code> is missing in the Item model
BBecause <code>has_many</code> requires a foreign key in the Order model
CBecause <code>order</code> is not saved, so <code>items</code> association is empty and <code>first</code> returns nil, causing <code>nil.name</code> error
DBecause <code>order.items</code> returns a string, not a collection
Attempts:
2 left
💡 Hint
What happens when you call a method on nil?