Top 25 Ruby on Rails Interview Questions and Answers
Ruby on Rails is one of the most popular web application frameworks, known for its simplicity, flexibility, and developer-friendly conventions. Whether you’re a beginner or an experienced developer preparing for technical interviews, it’s crucial to have a solid understanding of Rails’ core concepts, best practices, and commonly asked interview questions. In this post, we’ve compiled the top 25 Ruby on Rails interview questions with detailed answers and real-world examples to help you sharpen your skills and ace your next interview. These questions cover everything from MVC architecture, Active Record, migrations, routing, to more advanced concepts like polymorphic associations and caching.
1. What is Ruby on Rails?
Ruby on Rails, also called Rails, is an open-source web application framework written in Ruby. It follows the Model-View-Controller (MVC) pattern and emphasizes Convention over Configuration (CoC) and Don’t Repeat Yourself (DRY) principles.
2. What are the main components of Ruby on Rails?
- Models: Handle data and business logic.
- Views: Display data to the user.
- Controllers: Handle the flow between models and views.
- Router: Maps URLs to controllers.
- Active Record: ORM layer.
- Action View: Template system.
- Action Controller: Controller logic.
3. Explain MVC architecture in Rails?
- Model: Interacts with the database.
- View: Handles presentation logic.
- Controller: Handles incoming HTTP requests, processes them, and returns the response.
Example:
# Controller
class UsersController < ApplicationController
def index
@users = User.all
end
end
# View: users/index.html.erb
<% @users.each do |user| %>
<p><%= user.name %></p>
<% end %>
4. What is Active Record in Rails?
Active Record is the ORM in Rails that maps database tables to Ruby classes, providing CRUD operations.
Example:
User.find(1)
User.create(name: ‘John’)
5. What are migrations in Rails?
Migrations are Ruby classes used to make changes to the database schema.
Example:
class AddAgeToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :age, :integer
end
end
6. What is the Rails console?
A command-line interface to interact with the Rails application, used for testing and debugging.
rails console
7. What are callbacks in Rails?
Callbacks are hooks into an object’s lifecycle, allowing code to run automatically at certain moments.
Example:
class User < ApplicationRecord
before_save :normalize_name
def normalize_name
self.name = name.capitalize
end
end
8. Difference between form_for and form_with in Rails?
- form_for is older, now soft-deprecated.
- form_with is the modern way and uses remote AJAX by default unless specified.
9. Explain RESTful routes in Rails?
Rails uses RESTful conventions to map HTTP verbs to controller actions.
HTTP Verb | Path | Action |
GET | /users | index |
POST | /users | create |
GET | /users/:id | show |
10. What is the purpose of strong_parameters in Rails?
Used to prevent mass-assignment vulnerabilities.
params.require(:user).permit(:name, :email)
11. What are concerns in Rails?
Reusable modules that can be mixed into models or controllers.
Example:
module Archivable
def archive
update(archived: true)
end
end
class Post < ApplicationRecord
include Archivable
end
12. What is CSRF protection in Rails?
Rails uses tokens to protect forms from Cross-Site Request Forgery attacks.
<%= form_with … do %>
<%= csrf_meta_tags %>
<% end %>
13. Explain before_action in controllers?
It runs specified methods before controller actions.
before_action :authenticate_user
14. How do you validate models in Rails?
class User < ApplicationRecord
validates :email, presence: true, uniqueness: true
end
15. What is the difference between render and redirect_to?
- render renders a view.
- redirect_to sends an HTTP redirect to a new URL.
16. What is the asset pipeline in Rails?
Manages and serves static assets (CSS, JS, images) in a unified way.
17. Explain eager loading and lazy loading in Rails?
- Eager loading: Loads associated records immediately (includes).
- Lazy loading: Loads associated records only when called.
Example:
User.includes(:posts).all
18. What is polymorphic association in Rails?
A model can belong to more than one other model using a single association.
Example:
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
end
19. Explain STI (Single Table Inheritance) in Rails?
Different models share a single table, using a type column.
Example:
class Animal < ApplicationRecord; end
class Dog < Animal; end
class Cat < Animal; end
20. Difference between has_many :through and has_and_belongs_to_many?
- has_many :through uses an explicit join model.
- has_and_belongs_to_many uses a direct join table.
21. Explain scopes in Rails?
Reusable query fragments.
scope :active, -> { where(active: true) }
22. What is rake and rails commands difference?
- rake used to run tasks.
- rails is used to generate code, run the server, and manage tasks.
- Rails 5+ merged most rake tasks into rails.
23. What is caching in Rails?
Speeds up responses by storing data.
<% cache @product do %>
<%= render @product %>
<% end %>
24. Explain Action Cable in Rails?
Action Cable integrates WebSockets for real-time features in Rails.
25. What are the different types of caching in Rails?
- Page caching
- Action caching
- Fragment caching
- Low-level caching (using Rails.cache)
Mastering Ruby on Rails interview questions is essential for any web developer aiming to build scalable and maintainable applications using this powerful framework. With this comprehensive list of top Rails interview questions and answers, you are now better equipped to tackle both basic and advanced topics in your interviews. Remember, continuous practice, exploring Rails documentation, and building real-world projects are the keys to reinforcing your knowledge. Bookmark this guide as your go-to resource for Rails interview preparation and stay ahead in your web development career.