This principle helps you write less setup code by following common rules. Rails knows what to do if you follow its naming and structure.
Convention over configuration principle in Ruby on Rails
No special code syntax; it means following Rails naming and folder rules.For example, a model named Article uses a database table named articles automatically.
Controllers named ArticlesController handle views in app/views/articles/ folder by default.
articles automatically.# Model file: app/models/article.rb class Article < ApplicationRecord end
app/views/articles/index.html.erb by default.# Controller file: app/controllers/articles_controller.rb class ArticlesController < ApplicationController def index @articles = Article.all end end
articles matching the model Article.# Migration file: db/migrate/20240101000000_create_articles.rb class CreateArticles < ActiveRecord::Migration[7.0] def change create_table :articles do |t| t.string :title t.text :content t.timestamps end end end
This example shows a Post model, a controller PostsController, and a view in app/views/posts/index.html.erb. Rails connects them automatically because of the naming conventions.
# app/models/post.rb class Post < ApplicationRecord end # app/controllers/posts_controller.rb class PostsController < ApplicationController def index @posts = Post.all end end # app/views/posts/index.html.erb <% @posts.each do |post| %> <h2><%= post.title %></h2> <p><%= post.content %></p> <% end %>
Following Rails conventions saves time and reduces errors.
You can override conventions, but it requires extra configuration.
Always name files, classes, and folders carefully to match Rails expectations.
Rails uses conventions to guess what you want, so you write less setup code.
Models, controllers, views, and database tables follow naming rules to connect automatically.
Following these rules helps you build apps faster and with fewer mistakes.
Practice
Convention over configuration principle in Rails primarily help with?Solution
Step 1: Understand the principle's goal
Convention over configuration means Rails guesses what you want if you follow its naming rules.Step 2: Identify the main benefit
This reduces the need to write extra setup or configuration code manually.Final Answer:
Reducing the amount of setup code by following naming rules -> Option BQuick Check:
Convention over configuration = less setup code [OK]
- Confusing it with performance optimization
- Thinking it forces more configuration files
- Assuming it auto-generates UI designs
Solution
Step 1: Recall Rails naming conventions for models
Model class names are singular and start with a capital letter, e.g., User.Step 2: Check syntax correctness
class User < ApplicationRecord uses correct class name capitalization and inherits from ApplicationRecord.Final Answer:
class User < ApplicationRecord -> Option AQuick Check:
Model class names are singular and capitalized [OK]
- Using plural or lowercase class names
- Not capitalizing the class name
- Using all uppercase letters
Article, which database table name will Rails expect by convention?Solution
Step 1: Recall Rails table naming convention
Rails expects database tables to be plural lowercase versions of model names.Step 2: Apply to the model 'Article'
The plural lowercase form is 'articles'.Final Answer:
articles -> Option AQuick Check:
Model 'Article' maps to table 'articles' [OK]
- Using singular table names
- Using capitalized table names
- Adding extra suffixes like '_table'
class Person < ApplicationRecord but named the database table people_data. What is the main issue here?Solution
Step 1: Understand default table naming
Rails expects the table name to be the plural form of the model name, here 'people'.Step 2: Check mismatch impact
Since the table is named 'people_data', Rails won't find it automatically and will raise an error unless explicitly told.Final Answer:
Rails will raise an error because the table name does not follow convention -> Option CQuick Check:
Table name mismatch causes error unless configured [OK]
- Assuming Rails guesses any table name
- Thinking Rails silently ignores the model
- Believing Rails auto-corrects table names
Solution
Step 1: Recall controller naming conventions
Controllers are plural, camel case with 'Controller' suffix, e.g., BlogPostsController.Step 2: Recall file path conventions
File names are snake_case plural with '_controller.rb' suffix, e.g., blog_posts_controller.rb.Final Answer:
class BlogPostsController < ApplicationController in app/controllers/blog_posts_controller.rb -> Option DQuick Check:
Controller class plural + Controller suffix, file snake_case plural [OK]
- Using singular controller class names
- Incorrect file naming or casing
- Omitting 'Controller' suffix
Ready to prove it?
Pass the test and this pattern moves unknown
