0
0
Ruby on Railsframework~10 mins

Why models represent data in Ruby on Rails - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why models represent data
User interacts with app
Controller receives request
Controller asks Model for data
Model talks to Database
Database returns data to Model
Model sends data to Controller
Controller sends data to View
View shows data to User
This flow shows how models act as the middleman between the database and the app, holding and managing data.
Execution Sample
Ruby on Rails
class User < ApplicationRecord
end

user = User.find(1)
puts user.name
This code fetches a user record from the database and prints the user's name.
Execution Table
StepActionModel StateDatabase InteractionOutput
1Call User.find(1)No user loadedQuery database for user with id=1No output yet
2Database returns user dataUser object created with id=1, name='Alice'Data fetchedNo output yet
3Access user.nameUser object with name='Alice'No database callReturns 'Alice'
4puts user.nameUser object unchangedNo database callPrints 'Alice' to console
5End of codeUser object existsNo database callExecution stops
💡 Code ends after printing the user's name
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
usernilnilUser(id=1, name='Alice')User(id=1, name='Alice')User(id=1, name='Alice')
Key Moments - 3 Insights
Why does calling User.find(1) not immediately print anything?
Because User.find(1) only fetches data and creates a model object; printing happens only when puts user.name is called (see execution_table step 4).
Is the model the same as the database?
No, the model is a Ruby object that holds data from the database and adds behavior; the database stores the raw data (see execution_table steps 1 and 2).
Why do we use models instead of talking directly to the database?
Models organize data and logic in one place, making code cleaner and easier to maintain (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'user' after step 2?
AUser object with id=1 and name='Alice'
Bnil
CUser object with no data
DString 'Alice'
💡 Hint
Check the 'Model State' column in execution_table row for step 2
At which step does the database return data to the model?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Database Interaction' column in execution_table
If we change 'puts user.name' to 'puts user.id', what output will change?
AOutput will be 'Alice' still
BOutput will be '1' instead of 'Alice'
CNo output will be printed
DCode will error
💡 Hint
Refer to the 'Output' column in execution_table step 4 and variable_tracker for user attributes
Concept Snapshot
Models in Rails represent data from the database.
They act as Ruby objects holding data and behavior.
Use Model.find(id) to get data as objects.
Models keep app code clean and organized.
They separate data logic from views and controllers.
Full Transcript
In Rails, models represent data by acting as Ruby objects that connect to the database. When the app needs data, the controller asks the model, which queries the database and returns data wrapped in an object. This object holds the data and can have methods to work with it. For example, calling User.find(1) fetches the user with id 1 from the database and creates a User object. Accessing user.name gets the name attribute from that object. Models help keep data logic separate from the user interface and controller code, making the app easier to build and maintain.