Interview Questions

Get ready for your next interview with our comprehensive question library

Ruby on Rails Interview Questions

Filter by Difficulty

1.

What is Ruby on Rails and what is the MVC pattern?

beginner

Ruby on Rails is a server-side web application framework written in Ruby that follows the Model-View-Controller (MVC) architectural pattern. MVC separates the application into three interconnected components:

  • Model: Manages data and business logic, interacts with the database
  • View: Handles presentation layer and user interface
  • Controller: Processes requests, coordinates between Model and View

Rails emphasizes convention over configuration and DRY (Don't Repeat Yourself) principles, making development faster and more maintainable.

2.

Explain the Rails directory structure and purpose of key folders

beginner

The main Rails directories include:

  • app/ - Core application code (models, views, controllers, helpers, assets)
  • config/ - Application configuration, routes, database settings
  • db/ - Database schema, migrations, seeds
  • lib/ - Extended modules and custom libraries
  • public/ - Static files directly served by web server
  • test/ or spec/ - Test files
  • vendor/ - Third-party code
  • Gemfile - Gem dependencies specification
3.

What is the difference between symbols and strings in Ruby, and why does Rails prefer symbols for hash keys?

beginner

Symbols are immutable and stored in memory only once, while strings create new objects each time. Rails prefers symbols because:

  • Memory efficiency - symbols are stored once
  • Performance - faster comparison operations
  • Immutability - prevents accidental modification
# Symbol - same object_id
:name.object_id == :name.object_id  # true

# String - different objects
"name".object_id == "name".object_id  # false
4.

What is the Rails Convention over Configuration principle?

beginner

Convention over Configuration means Rails makes assumptions about what you want to do and how you'll do it, rather than requiring you to specify every detail. Examples include:

  • Model User maps to database table users
  • Controller UsersController handles /users routes
  • Primary keys are named id
  • Foreign keys follow pattern table_id

This reduces the amount of code developers need to write and maintains consistency across projects.

ActiveRecord & Database

5.

Explain database migrations in Rails

beginner

Migrations are Ruby classes that allow you to evolve your database schema over time in a consistent way. They provide:

  • Version control for database schema
  • Team collaboration on schema changes
  • Database-agnostic schema modifications
  • Rollback capabilities
class CreateUsers < ActiveRecord::Migration[7.0]
  def change
    create_table :users do |t|
      t.string :email
      t.timestamps
    end
  end
end
6.

What's the difference between `find`, `find_by`, and `where`?

beginner
  • find: Finds by primary key, raises ActiveRecord::RecordNotFound if not found
  • find_by: Finds first record matching conditions, returns nil if not found
  • where: Returns ActiveRecord::Relation with all matching records
User.find(1)                    # Raises error if not found
User.find_by(email: "a@b.com")  # Returns nil if not found
User.where(active: true)        # Returns Relation (chainable)
7.

What is the difference between `render` and `redirect_to`?

beginner
  • render: Renders a view template without a new HTTP request, maintains instance variables
  • redirect_to: Sends HTTP redirect response, triggers new request, loses instance variables
def create
  @user = User.new(user_params)
  if @user.save
    redirect_to @user  # New request to show action
  else
    render :new  # Renders new template with @user errors
  end
end
8.

What are the differences between `rails console` options?

beginner

Rails console options:

  • rails console or rails c: Standard console
  • rails console --sandbox: Rolls back changes on exit
  • rails console production: Production environment console
  • rails dbconsole or rails db: Direct database console
# Sandbox mode - all changes rolled back
rails console --sandbox

# Production console
RAILS_ENV=production rails console
9.

How does Rails handle different environments and what are they used for?

beginner

Rails provides three default environments:

  • Development: Reloading, verbose errors, debugging tools
  • Test: Isolated testing, fixtures, transaction rollback
  • Production: Optimized performance, caching, error monitoring
# Check current environment
Rails.env.development?  # true/false
Rails.env  # "development"

# Environment-specific code
if Rails.env.production?
  # Production-only code
end

# Custom environments
RAILS_ENV=staging rails server

Each environment has its own configuration file in config/environments/ and database configuration.

10.

What is ActiveRecord and how does it implement the Active Record pattern?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
11.

Explain different types of associations in Rails

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
12.

What is the N+1 query problem and how do you solve it?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
13.

What are scopes in ActiveRecord and how do you use them?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
14.

Explain callbacks in ActiveRecord and their order of execution

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

What are validations and how do custom validations work?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

What are strong parameters and why are they important?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

Explain the Rails routing system and RESTful routes

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What are before_action filters and how do they work?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

How do you handle different response formats in Rails controllers?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

Explain nested routes and when to use them

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 65 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime