Bird
Raised Fist0
Ruby on Railsframework~20 mins

CRUD operations through models in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rails Model Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Rails model code output after creation?
Consider the following Rails model code snippet. What will be the value of user.name after running this code?
Ruby on Rails
user = User.create(name: "Alice", age: 30)
user.name
A"Alice"
Bnil
C""
DRaises ActiveRecord::RecordInvalid error
Attempts:
2 left
💡 Hint
The create method saves the record and returns the object with attributes set.
❓ state_output
intermediate
2:00remaining
What is the value of user.persisted? after new and save?
Given the following code, what will user.persisted? return after the save call?
Ruby on Rails
user = User.new(name: "Bob")
saved = user.save
user.persisted?
Afalse
Btrue
Cnil
DRaises NoMethodError
Attempts:
2 left
💡 Hint
The save method returns true if the record was saved successfully.
🔧 Debug
advanced
2:00remaining
Why does this update raise an error?
Examine the code below. Why does user.update(name: nil) raise an error if the model validates presence of name?
Ruby on Rails
user = User.find(1)
user.update(name: nil)
AValidation fails because name cannot be nil, so update returns false and does not raise error
Bupdate raises ActiveRecord::RecordInvalid because validations fail and update! was called
Cupdate raises NoMethodError because name is nil
Dupdate raises ActiveRecord::RecordNotFound because user id 1 does not exist
Attempts:
2 left
💡 Hint
Check if update or update! is used and how validations behave.
📝 Syntax
advanced
2:00remaining
Which option correctly deletes a record by id?
Which of the following Rails model commands correctly deletes the user with id 5 from the database?
AUser.delete(5)
BUser.remove(5)
CUser.destroy(5)
DUser.destroy_by(id: 5)
Attempts:
2 left
💡 Hint
Consider which methods trigger callbacks and which do not.
🧠 Conceptual
expert
2:00remaining
What is the result of chaining update and reload?
Given this code, what will user.name be after the last line?
Ruby on Rails
user = User.create(name: "Carol")
user.update(name: "Diana")
user.reload
user.name
Anil
B"Carol"
CRaises ActiveRecord::RecordNotFound
D"Diana"
Attempts:
2 left
💡 Hint
The reload method refreshes the object from the database.

Practice

(1/5)
1. Which Rails model method is used to add a new record to the database?
easy
A. create
B. find
C. update
D. destroy

Solution

  1. Step 1: Understand CRUD operations

    CRUD means Create, Read, Update, Delete. Each operation has a matching Rails model method.
  2. Step 2: Match method to action

    create adds a new record, find reads, update changes, and destroy deletes.
  3. Final Answer:

    create -> Option A
  4. Quick Check:

    Create = create [OK]
Hint: Create means adding new data, so use create [OK]
Common Mistakes:
  • Confusing find with create
  • Using update to add new records
  • Trying to delete with create
2. Which of the following is the correct syntax to update a user's email in Rails?
easy
A. user.change(email = 'new@example.com')
B. user.update(email: 'new@example.com')
C. user.modify(email: 'new@example.com')
D. user.edit(email => 'new@example.com')

Solution

  1. Step 1: Recall Rails update syntax

    The correct method to change attributes is update with a hash of attributes.
  2. Step 2: Check syntax correctness

    user.update(email: 'new@example.com') uses symbol keys and colon, which is correct Ruby syntax.
  3. Final Answer:

    user.update(email: 'new@example.com') -> Option B
  4. Quick Check:

    Update syntax = user.update(hash) [OK]
Hint: Use update with symbol keys and colon for attributes [OK]
Common Mistakes:
  • Using assignment (=) inside method arguments
  • Using non-existent methods like change or modify
  • Wrong hash syntax like email => instead of email:
3. Given the code:
user = User.find(1)
user.destroy
puts User.exists?(1)

What will be printed?
medium
A. An error
B. true
C. nil
D. false

Solution

  1. Step 1: Understand what destroy does

    destroy deletes the record from the database permanently.
  2. Step 2: Check if record exists after deletion

    User.exists?(1) returns false because the user with ID 1 was deleted.
  3. Final Answer:

    false -> Option D
  4. Quick Check:

    Destroy removes record, so exists? returns false [OK]
Hint: Destroy deletes record; check existence returns false after [OK]
Common Mistakes:
  • Thinking destroy only marks record inactive
  • Assuming exists? returns true after deletion
  • Confusing destroy with delete (which skips callbacks)
4. Identify the error in this code snippet:
user = User.find_by(id: 10)
user.update(name: 'Alice')
medium
A. No error, code works fine
B. find_by should be find to work
C. find_by returns nil if no record found, so calling update on nil causes error
D. update method does not accept hash arguments

Solution

  1. Step 1: Understand find_by behavior

    find_by returns nil if no matching record is found.
  2. Step 2: Check method call on nil

    Calling update on nil causes a NoMethodError because nil has no update method.
  3. Final Answer:

    find_by returns nil if no record found, so calling update on nil causes error -> Option C
  4. Quick Check:

    Nil object has no update method [OK]
Hint: Check if find_by returns nil before calling methods [OK]
Common Mistakes:
  • Assuming find_by always returns a record
  • Thinking update cannot take hash arguments
  • Confusing find and find_by behavior
5. You want to update multiple users' status to 'active' only if their current status is 'pending'. Which Rails code correctly does this in one query?
hard
A. User.where(status: 'pending').update_all(status: 'active')
B. User.update_all(status: 'active').where(status: 'pending')
C. User.find_each { |u| u.update(status: 'active') if u.status == 'pending' }
D. User.all.update(status: 'active') if User.status == 'pending'

Solution

  1. Step 1: Filter users with status 'pending'

    User.where(status: 'pending') selects only users with 'pending' status.
  2. Step 2: Update all selected users at once

    update_all(status: 'active') updates the status for all filtered users in one database query.
  3. Final Answer:

    User.where(status: 'pending').update_all(status: 'active') -> Option A
  4. Quick Check:

    Filter then update_all = efficient bulk update [OK]
Hint: Use where to filter, then update_all to bulk update [OK]
Common Mistakes:
  • Calling update_all before filtering
  • Using find_each which is slower for bulk updates
  • Trying to chain where after update_all
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard