0
0
Ruby on Railsframework~10 mins

CRUD operations through models in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CRUD operations through models
Create Model Instance
Save to Database
Read from Database
Update Model Attributes
Save Changes
Delete Model Instance
End
This flow shows how a model instance is created, saved, read, updated, and deleted in Rails.
Execution Sample
Ruby on Rails
user = User.new(name: "Alice")
user.save
user = User.find_by(name: "Alice")
user.name = "Alice Smith"
user.save
user.destroy
This code creates a user, saves it, reads it back, updates the name, and then deletes the user.
Execution Table
StepActionModel StateDatabase StateResult
1User.new(name: "Alice")User instance with name='Alice', not savedNo user recordInstance created in memory
2user.saveUser instance savedUser record with name='Alice' createdUser saved to DB
3User.find_by(name: "Alice")User instance with name='Alice' loadedUser record with name='Alice' existsUser loaded from DB
4user.name = "Alice Smith"User instance name changed to 'Alice Smith'User record still 'Alice'Instance updated, DB not yet changed
5user.saveUser instance saved with name='Alice Smith'User record updated to 'Alice Smith'DB updated
6user.destroyUser instance marked for deletionUser record deletedUser removed from DB
7User.find_by(name: "Alice Smith")No user foundNo user recordReturns nil, user deleted
💡 User record deleted, no further operations possible on this user
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
usernilUser(name='Alice', unsaved)User(name='Alice', saved)User(name='Alice', loaded)User(name='Alice Smith', updated)User(name='Alice Smith', saved)User(deleted)User(deleted)
Key Moments - 3 Insights
Why does the user variable still exist after calling destroy?
The user variable still holds the Ruby object after destroy, but the database record is deleted (see step 6 and 7 in execution_table). The object is disconnected from the DB.
Does changing attributes immediately change the database?
No, changing attributes (step 4) updates the model instance in memory but requires save (step 5) to update DB. (Note: model.update combines attr= and save.)
What happens if we try to find the user after destroy?
The find_by returns nil because the record no longer exists in the database (step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of the user variable after step 3?
AUser instance with name='Alice' loaded
BUser instance with name='Alice Smith' updated
CUser instance marked for deletion
DNo user found
💡 Hint
Check the 'Model State' column at step 3 in the execution_table
At which step does the database record change from 'Alice' to 'Alice Smith'?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Database State' column for when the name updates in the execution_table
If we skip calling user.save after step 4, what would happen?
AThe user variable would be nil
BThe database record would not change
CThe database record would update automatically
DThe user record would be deleted
💡 Hint
Refer to the explanation in key_moments about changing attributes and save behavior
Concept Snapshot
Rails CRUD through models:
- Create: Model.new + save
- Read: Model.find or find_by
- Update: update (calls save internally) or attr= + save
- Delete: destroy
Changes affect DB immediately after save or destroy
Model instance exists in Ruby memory separately from DB
Full Transcript
This visual execution traces CRUD operations in Rails models. First, a User instance is created in memory with User.new. Then, calling save stores it in the database. Reading uses find_by to load the user from the database. Updating the user changes attributes and saves the changes to the database. Destroy deletes the record from the database but the Ruby object remains until discarded. Trying to find the user after destroy returns nil because the record no longer exists. This shows how model instances and database records interact step-by-step.