Basic Ruby on Rails Interview Questions
These foundational questions cover core concepts of Ruby on Rails, suitable for freshers and candidates with 1-3 years of experience.
1. What is Ruby on Rails and what are its key principles?
Ruby on Rails is a web application framework written in Ruby that follows the Model-View-Controller (MVC) architectural pattern. Its key principles are Convention over Configuration (CoC) and Don’t Repeat Yourself (DRY), which reduce setup time and code duplication.[1]
2. Explain the MVC architecture in Ruby on Rails.
In MVC, Models handle data and business logic, Views manage the user interface, and Controllers process incoming requests, interact with models, and render appropriate views.[3]
3. What are Rails migrations and how do you create one?
Rails migrations are Ruby classes that modify the database schema. Create one using rails generate migration MigrationName, then run rails db:migrate to apply changes.[4]
4. What is Active Record in Ruby on Rails?
Active Record is Rails’ Object-Relational Mapping (ORM) system that maps Ruby objects to database tables, providing methods to query and manipulate data without writing SQL.[1]
5. What naming conventions does Rails follow?
Rails uses conventions like plural table names (users), singular class names (User), snake_case for files and methods, and CamelCase for classes.[4]
6. How do you generate a new Rails application?
Use the command rails new app_name --database=postgresql to create a new Rails application with specified database configuration.[1]
7. What is the difference between load and require in Ruby?
load executes a file every time it’s called, while require loads a file only once and is used for libraries and gems.[4]
8. What are instance, class, and global variables in Ruby?
Instance variables start with @ (@name), class variables with @@ (@@count), and global variables with $ ($global).[4]
9. What is the Rails asset pipeline?
The asset pipeline organizes and processes CSS, JavaScript, and image assets, concatenating, minifying, and compressing them for production.[5]
10. How do you run Rails server and console?
Run server with rails server or rails s, and console with rails console or rails c.[1]
Intermediate Ruby on Rails Interview Questions
These questions target developers with 1-3 years experience, focusing on practical implementation and common patterns at companies like Zoho and Atlassian.
11. What are Rails callbacks and name some common ones?
Callbacks execute code before, after, or around specific events in the Active Record lifecycle. Common ones include before_save, after_create, before_update, and after_commit.[1]
class User < ApplicationRecord
before_save :normalize_name
private
def normalize_name
self.name = name.downcase.titleize
end
end
12. Explain Rails associations with examples.
Rails supports belongs_to, has_one, has_many, and has_many :through. Example: A Post has_many Comments, and Comment belongs_to Post.[5]
13. What is 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
14. How do you implement authentication in Rails?
Create a concern module with authentication methods and include it in the User model using include Authentication.[1]
15. What is the difference between include and extend in Ruby?
include makes module methods available as instance methods, while extend makes them available as class methods.[5]
16. Explain partials in Rails views.
Partials are reusable view templates stored as _partial.html.erb and rendered with <%= render 'partial' %> to avoid code duplication.[3]
17. How do you handle strong parameters in controllers?
Use params.require(:user).permit(:name, :email) to whitelist permitted parameters for mass assignment security.[1]
18. What are Rails generators and how do you use them?
Generators create boilerplate code. Example: rails generate model User name:string email:string creates model, migration, and test files.[1]
19. Explain polymorphic associations.
Polymorphic associations allow a model to belong_to multiple models using taggable_type and taggable_id columns.[5]
20. What is the Rails console useful for?
Rails console provides an interactive Ruby environment connected to your app’s database for testing code, querying data, and debugging.[1]
Advanced Ruby on Rails Interview Questions
These scenario-based questions are for 3-6 years experienced developers working at product companies like Salesforce and Adobe.
21. How would you implement caching in a Rails application?
Use fragment caching with <% cache @article do %> or low-level caching with Rails.cache.fetch to store expensive computations.[3]
22. Explain ActiveSupport::Concern with an example.
Concerns group related functionality into reusable modules. At Atlassian, you might create a Noteable concern for models supporting user notes.[2]
module Noteable
extend ActiveSupport::Concern
included do
has_many :notes, as: :noteable
end
end
23. How do you optimize N+1 queries in Rails?
Use includes, eager_load, or preload to fetch associations in a single query. Example: User.includes(:posts).all.[1]
24. What is CSRF protection in Rails and how does it work?
CSRF (Cross-Site Request Forgery) protection uses authenticity tokens in forms to verify requests originate from your application.[7]
25. How do you implement background jobs in Rails?
Use Active Job with queue adapters. Example: MyJob.perform_later(user) queues email delivery without blocking the request.[1]
26. Scenario: At Paytm, how would you secure an API endpoint?
Implement token-based authentication, rate limiting with middleware, and strong parameter filtering for all incoming requests.[1]
27. Explain metaprogramming for role-based methods.
Define dynamic methods using define_method to create role checkers like admin?, sales? from an array of roles.[2]
['admin', 'sales'].each do |role|
define_method "#{role}?" do
self.role == role
end
end
28. How do you handle database transactions in Rails?
Use ActiveRecord::Base.transaction to ensure multiple operations succeed or fail together. Example: creating related records atomically.[1]
29. What are service objects and when to use them?
Service objects encapsulate complex business logic outside models/controllers. Use for operations spanning multiple models like order processing at Flipkart.[1]
30. Scenario: Optimize a slow Swiggy order listing page.
Add pagination with will_paginate, cache fragments, eager load associations, and add database indexes on frequently queried columns.[3]