0
0
Ruby on Railsframework~5 mins

CRUD operations through models in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does CRUD stand for in Rails models?
CRUD stands for Create, Read, Update, and Delete. These are the four basic actions you can perform on data using Rails models.
Click to reveal answer
beginner
How do you create a new record using a Rails model?
Use Model.create(attributes) to create and save a new record in one step, or Model.new(attributes) followed by save to create it in two steps.
Click to reveal answer
beginner
How do you find a record by its ID in Rails?
Use Model.find(id) to get a record by its ID. It raises an error if not found. Use Model.find_by(attribute: value) to find by other attributes safely.
Click to reveal answer
beginner
How do you update an existing record in Rails?
Find the record first, then use update(attributes) to change and save it in one step, or assign attributes and call save separately.
Click to reveal answer
beginner
How do you delete a record using Rails models?
Use destroy on a model instance to delete it from the database. For example, record.destroy removes that record.
Click to reveal answer
Which method creates and saves a new record in one step?
AModel.new(attributes)
BModel.create(attributes)
CModel.update(attributes)
DModel.destroy
What happens if Model.find(id) does not find a record?
ARaises an error
BReturns an empty array
CCreates a new record
DReturns nil
Which method updates attributes and saves the record immediately?
Anew(attributes)
Bsave
Cupdate(attributes)
Ddestroy
How do you delete a record from the database?
AModel.destroy_all
Brecord.delete
CModel.delete
Drecord.destroy
Which method safely finds a record by attribute without raising an error if not found?
AModel.find_by(attribute: value)
BModel.create
CModel.find(id)
DModel.update
Explain how to perform all four CRUD operations using Rails models with simple examples.
Think about how you add, look up, change, and remove data in a database using Rails.
You got /4 concepts.
    Describe the difference between Model.find and Model.find_by in Rails.
    Consider what happens when the record does not exist.
    You got /4 concepts.