0
0
Ruby on Railsframework~10 mins

Convention over configuration principle in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Convention over configuration principle
Start Rails App
Look for default folders
Use default file names
Apply default behaviors
Run app with minimal setup
Override only if needed
App runs smoothly
Rails uses default names and folders so you write less setup code. It runs with these defaults unless you change them.
Execution Sample
Ruby on Rails
class User < ApplicationRecord
end
Defines a User model that Rails links automatically to the users table without extra config.
Execution Table
StepActionEvaluationResult
1Define User model classUser inherits from ApplicationRecordRails expects users table
2No table name specifiedRails uses 'users' as table name by conventionNo config needed
3Call User.allRails queries 'users' tableReturns all user records
4Add new model ProductNo config for table nameRails uses 'products' table
5Override table name in modelSpecify self.table_name = 'my_products'Rails uses 'my_products' instead
6Run appRails uses conventions or overridesApp runs with minimal config
💡 Rails stops looking for config when conventions are met or explicit overrides are given
Variable Tracker
VariableStartAfter Step 2After Step 5Final
Model ClassUser < ApplicationRecordUses 'users' tableUses 'my_products' table if overriddenFinal table name depends on override
Table NameNone specified'users' by convention'my_products' if overriddenUsed by Rails for queries
Key Moments - 3 Insights
Why does Rails not need explicit table names for models?
Rails assumes table names by pluralizing model class names, as shown in execution_table step 2 and 3.
What happens if I want to use a different table name?
You can override the table name in the model, as shown in execution_table step 5, so Rails uses your specified name.
Does Rails require configuration files for every model?
No, Rails uses conventions to reduce config, only requiring overrides when deviating from defaults, as shown in steps 1-6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what table name does Rails use for the User model by default?
A'User'
B'user'
C'users'
D'users_table'
💡 Hint
Check step 2 and 3 in the execution_table where Rails uses 'users' by convention.
At which step does Rails switch to using a custom table name?
AStep 5
BStep 3
CStep 1
DStep 6
💡 Hint
Look at step 5 where the override self.table_name is set.
If you add a new model Product without configuration, what table will Rails query?
A'product'
B'products'
C'Product'
D'product_table'
💡 Hint
See step 4 where Rails uses pluralized model names as table names.
Concept Snapshot
Convention over configuration means Rails uses default names and folders so you write less setup.
Model names map to plural table names automatically.
You only configure when deviating from defaults.
This speeds up development and reduces errors.
Example: class User < ApplicationRecord uses 'users' table by default.
Full Transcript
The convention over configuration principle in Rails means the framework assumes default settings so developers don't have to write extra setup code. For example, a model named User automatically connects to a database table named users without specifying it. This reduces the need for configuration files or explicit declarations. If you want to use a different table name, you can override it in the model. This approach lets Rails apps run smoothly with minimal setup, speeding development and avoiding mistakes. The execution steps show how Rails looks for defaults, applies them, and only uses configuration when necessary.