All interview guides

Ruby on Rails Interview Questions and Answers

24 questions that come up in Ruby on Rails technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What is Ruby on Rails?

Beginner

Answer: Web application framework following MVC pattern

Ruby on Rails is a server-side web application framework written in Ruby. Follows MVC (Model-View-Controller) pattern. Emphasizes convention over configuration and DRY principles.

2. What is service object pattern?

Advanced

Answer: Extracts business logic into dedicated classes

Service objects encapsulate business logic. Keep controllers, models thin. app/services/. Single responsibility. call method convention. Better organization, testability.

3. What are associations?

Intermediate

Answer: Relationships between models: has_many, belongs_to, has_one

Associations define model relationships. has_many, belongs_to, has_one, has_and_belongs_to_many, has_many :through. Provide convenient methods. Define in model classes.

4. What is belongs_to?

Intermediate

Answer: Defines one-to-one relationship from child side

belongs_to defines child side: class Post < ApplicationRecord; belongs_to :user; end. Expects user_id foreign key. Provides user method. Singular.

5. What does MVC stand for?

Beginner

Answer: Model-View-Controller

MVC: Model (data/business logic), View (presentation), Controller (handles requests, coordinates). Separation of concerns. Core architectural pattern in Rails.

6. What is form object pattern?

Advanced

Answer: Handles complex forms with multiple models

Form objects handle complex forms. Coordinate multiple models. Include ActiveModel::Model. Validations, save logic. Keep controllers clean. Better than accepts_nested_attributes_for.

7. What is query object pattern?

Advanced

Answer: Encapsulates complex queries

Query objects extract complex queries. Keep models clean. Reusable, testable. app/queries/. Return ActiveRecord::Relation for chaining. Single responsibility.

8. What is has_many?

Intermediate

Answer: Defines one-to-many relationship from parent side

has_many defines parent side: class User < ApplicationRecord; has_many :posts; end. Provides posts collection. Plural. Can add dependent options.

9. How do you create a new Rails application?

Beginner

Answer: rails new app_name

Create app with rails new app_name. Options: --database=postgresql, --skip-test. Creates complete directory structure. Run bundle install automatically.

10. What is has_many :through?

Intermediate

Answer: Many-to-many through join model

has_many :through creates many-to-many via join model. has_many :comments, through: :posts. More flexible than HABTM. Can add attributes to join.

11. What is decorator pattern?

Advanced

Answer: Adds presentation logic without modifying model

Decorators add presentation logic. Use Draper gem. Keep models clean. Delegate to model. app/decorators/. Alternative to bloated helpers.

12. What is the Rails directory structure?

Beginner

Answer: app/, config/, db/, public/, test/, lib/, etc.

Main directories: app/ (MVC code), config/ (configuration), db/ (database), public/ (static files), test/ or spec/ (tests). Convention over configuration.

13. What is API mode?

Advanced

Answer: Rails configured for API-only applications

API mode: rails new app --api. No views, assets. Lighter middleware. ApplicationController < ActionController::API. JSON responses. Use for backend APIs.

14. What are validations?

Intermediate

Answer: Ensure data integrity before saving to database

Validations ensure data integrity: validates :name, presence: true. Run before save. Prevent invalid data. Add errors to model. Display in views.

15. What is a model?

Beginner

Answer: Represents data and business logic, inherits from ApplicationRecord

Model represents data/business logic. Inherits from ApplicationRecord (Active Record). Maps to database table. Handles validations, associations, queries.

16. What are common validations?

Intermediate

Answer: presence, uniqueness, length, format, numericality

Common: presence (required), uniqueness, length, format (regex), numericality, inclusion, exclusion. Custom validators possible. Multiple per field.

17. What is serialization?

Advanced

Answer: Converting models to JSON/XML

Serialization converts to JSON/XML. Use ActiveModel::Serializers, fast_jsonapi, jbuilder. Control output shape. API responses. Versioning consideration.

18. What is a controller?

Beginner

Answer: Handles requests, coordinates models and views

Controller receives requests, interacts with models, renders views. Inherits from ApplicationController. Actions are public methods. RESTful by convention.

19. What is GraphQL?

Advanced

Answer: Query language for APIs, alternative to REST

GraphQL provides flexible API queries. Use graphql-ruby gem. Single endpoint. Clients request exactly needed data. Alternative to REST. More complex but powerful.

20. What are callbacks?

Intermediate

Answer: Methods triggered at lifecycle events

Callbacks run at specific points: before_save, after_create, before_validation. Define in models. Use for side effects, computed fields. Can skip with save(validate: false).

21. What is a view?

Beginner

Answer: Presents data, typically HTML templates with embedded Ruby

Views render presentation. Typically ERB templates (.html.erb). Can access instance variables from controller. Layouts wrap views. Partials for reusable components.

22. How do you generate a model?

Beginner

Answer: rails generate model Name field:type

Generate model: rails generate model User name:string email:string. Creates model file, migration, test. Can use shorthand: rails g model. Specify field types.

23. What is database sharding?

Advanced

Answer: Horizontal partitioning across databases

Sharding splits data across databases. Horizontal scaling. Rails 6+ multi-database support. Connect to multiple databases. Complex but enables scaling.

24. What are scopes?

Intermediate

Answer: Named queries for reuse

Scopes are reusable queries: scope :published, -> { where(published: true) }. Chain scopes. Keep queries DRY. Use lambdas for arguments. Class methods alternative.

Ready to test yourself?

The full Ruby on Rails bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Ruby on Rails quiz