0
0
Ruby on Railsframework~20 mins

Why models represent data in Ruby on Rails - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Model Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do Rails models represent data?

In Rails, what is the main reason models represent data?

ATo handle user interface layout and styling
BTo manage HTTP requests and routing
CTo store and manage the application's data and business logic
DTo control background job processing
Attempts:
2 left
💡 Hint

Think about where the app keeps and organizes information.

component_behavior
intermediate
2:00remaining
What happens when you create a new model instance in Rails?

Consider this Rails code:

user = User.new(name: 'Alice')

What does this code do?

ACreates a new user record in the database immediately
BCreates a new User object in memory without saving to the database yet
CDeletes an existing user named Alice
DUpdates the name of all users to 'Alice'
Attempts:
2 left
💡 Hint

Think about when data is saved to the database.

state_output
advanced
2:00remaining
What is the output after saving a model?

Given this Rails code:

user = User.new(name: 'Bob')
saved = user.save
puts saved

What will be printed?

AAn error is raised
Bfalse
Cnil
Dtrue
Attempts:
2 left
💡 Hint

Think about what save returns when successful.

📝 Syntax
advanced
2:00remaining
Which code correctly defines a model with a validation?

Which of these Rails model definitions correctly adds a validation for presence of a name?

Ruby on Rails
class User < ApplicationRecord
  # validation here
end
Avalidates :name, presence: true
Bvalidate :name, presence: true
Cvalidates_presence_of :name
Dvalidates :name, required: true
Attempts:
2 left
💡 Hint

Look for the correct Rails validation syntax.

🔧 Debug
expert
3:00remaining
Why does this model raise an error on save?

Given this model:

class Product < ApplicationRecord
  validates :price, numericality: { greater_than: 0 }
end

product = Product.new(price: -5)
product.save!

What error will this code raise?

AActiveRecord::RecordInvalid
BNoMethodError
CArgumentError
DTypeError
Attempts:
2 left
💡 Hint

Think about what happens when validations fail and save! is called.