Master Ruby on Rails Interviews: Basic to Advanced Questions
Prepare for your next Ruby on Rails interview with these 30 carefully curated questions covering basic concepts, intermediate practical scenarios, and advanced topics. Suitable for freshers, 1-3 years experience, and 3-6 years professionals working at companies like Zoho, Atlassian, and Swiggy.
Basic Ruby on Rails Questions (1-10)
1. What is Ruby on Rails and its core principle?
Ruby on Rails is a web application framework that follows the Model-View-Controller (MVC) architecture. Its core principle is Convention over Configuration (CoC), which means Rails provides sensible defaults so developers write less code.[1]
2. Explain the MVC architecture in Ruby on Rails.
In MVC, Model handles data logic and database interaction, View manages the user interface, and Controller processes requests, interacts with models, and renders views.[3]
3. What are Rails migrations?
Rails migrations are Ruby classes that allow you to create, modify, and maintain database schema in a version-controlled manner using Ruby code instead of SQL.[4]
4. What is Active Record in Ruby on Rails?
Active Record is Rails’ Object-Relational Mapping (ORM) layer that maps database tables to Ruby classes and rows to objects, simplifying database operations.[1]
5. Explain Rails naming conventions.
Rails follows conventions like plural table names (users), singular model names (User), snake_case for database columns (first_name), and camelCase for JavaScript.[4]
6. What is the difference between load and require in Ruby?
load executes a Ruby file every time it’s called, while require loads a library only once and prevents reloading the same file multiple times.[4]
7. How do you generate a new Rails application?
Use the command rails new app_name to generate a new Rails application with the standard directory structure and default configuration.[1]
8. What are Rails generators?
Rails generators are scripts that automatically create boilerplate code for models, controllers, migrations, and scaffolds to speed up development.[1]
9. Explain instance, class, and global variables in Ruby.
Instance variables start with @ (@name), class variables with @@ (@@count), and global variables with $ ($global).[4]
10. What is the Rails asset pipeline?
The asset pipeline organizes and processes CSS, JavaScript, and images into optimized files, enabling proper concatenation, minification, and serving of assets.[5]
Intermediate Ruby on Rails Questions (11-20)
11. What are Rails callbacks and name some common ones?
Rails callbacks execute code at specific points in an object’s lifecycle. Common ones include before_save, after_create, before_validation, and after_commit.[1]
class User < ApplicationRecord
before_save :set_default_role
private
def set_default_role
self.role ||= 'user'
end
end
12. Explain RESTful routing in Rails.
RESTful routing maps HTTP methods to controller actions: GET /articles (index), POST /articles (create), GET /articles/:id (show), etc.[3]
resources :articles
# Generates: GET /articles, POST /articles, GET /articles/:id, etc.
13. What is the difference between include and extend in Rails concerns?
include makes module methods available as instance methods, while extend makes them available as class methods.[5]
14. How do you implement authentication in Rails?
Create an Authentication concern with methods for login/logout, then include it in the User model and ApplicationController.[1]
module Authentication
extend ActiveSupport::Concern
included do
helper_method :current_user
end
end
15. What are Rails partials and how are they used?
Partials are reusable view components stored as _filename.html.erb. Render them using <%= render 'partial_name' %>.[3]
16. Explain has_many, belongs_to, and has_one associations.
has_many defines a one-to-many relationship, belongs_to the inverse, and has_one a one-to-one relationship.[5]
class User < ApplicationRecord
has_many :orders
has_one :profile
end
class Order < ApplicationRecord
belongs_to :user
end
17. What is a Rails concern and when should you use it?
Concerns group related functionality using ActiveSupport::Concern. Use them when multiple models/controllers share coherent methods like authentication or search.[1][2]
18. How do you handle strong parameters in Rails controllers?
Strong parameters prevent mass assignment vulnerabilities by whitelisting permitted attributes using permit and require.[1]
def user_params
params.require(:user).permit(:name, :email, :role)
end
19. What is polymorphism in Rails associations?
Polymorphic associations allow a model to belong to multiple other models using a single association with :as option.[5]
class Note < ApplicationRecord
belongs_to :notable, polymorphic: true
end
20. Explain Rails scopes.
Scopes define common query chains as named methods on models for reusable query logic.[1]
class Product < ApplicationRecord
scope :active, -> { where(active: true) }
scope :recent, -> { order(created_at: :desc) }
end
Advanced Ruby on Rails Questions (21-30)
21. What is CSRF protection in Rails?
Cross-Site Request Forgery (CSRF) protection prevents malicious sites from submitting forms. Rails automatically includes CSRF tokens in forms.[7]
22. Explain Rails caching strategies.
Rails supports page caching, action caching, fragment caching, and low-level caching to improve performance by storing expensive computations.[3]
23. How does Rails handle database transactions?
Use ActiveRecord::Base.transaction to ensure multiple database operations either all succeed or all fail atomically.[1]
ActiveRecord::Base.transaction do
user.update!(role: 'admin')
user.account.deactivate!
end
24. What are Rails background jobs and how are they implemented?
Background jobs handle long-running tasks asynchronously using Active Job with adapters like Sidekiq or Delayed Job.[1]
25. Explain the difference between save and save! in Active Record.
save returns true/false silently on validation failure, while save! raises an exception on failure.[1]
26. How do you optimize N+1 queries in Rails?
Use includes, eager_load, or preload to fetch associations in a single query instead of multiple queries.[1]
# N+1 query (avoid)
User.all.each { |u| u.posts }
# Optimized
User.includes(:posts).all.each { |u| u.posts }
27. What is Rails service layer and when to use it?
Service objects encapsulate complex business logic that doesn’t belong in models or controllers, keeping them thin.[1]
28. Explain Rails Action Cable for real-time features.
Action Cable provides WebSocket support for real-time features like live updates and chat applications in Rails.[1]
29. How do you implement API versioning in Rails?
Use namespaces in routes (namespace :api, path: '/api/v1' do) or versioned gems like api-versioning.[1]
30. What are Rails decorators and their use case?
Decorators (using draper gem) wrap models in views to add presentation logic without polluting models.[1]
Practice these Ruby on Rails interview questions to build confidence across all experience levels. Focus on understanding concepts deeply and practice coding scenarios regularly.