Posted in

Top 30 Ruby on Rails Interview Questions and Answers for All Experience Levels

Basic Ruby on Rails Interview Questions

1. What is Ruby on Rails and what are its core principles?

Ruby on Rails is a web application framework written in Ruby that follows the Model-View-Controller (MVC) architectural pattern. Its core 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 render the user interface, and Controllers process requests and coordinate between models and views. This separation makes applications more organized and maintainable.[2]

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 AddColumnToTable column:type, then run rails db:migrate to apply changes.[4]

4. What is the difference between find, find_by, and find_by! in Active Record?

find(id) retrieves a record by primary key or raises an error if not found. find_by(key: value) returns nil if not found, while find_by!(key: value) raises an error.[3]

5. What naming conventions does Rails follow?

Controllers use plural names (e.g., ArticlesController), models use singular (Article), tables use plural (articles), and foreign keys use singular_table_id (article_id).[4]

6. How do you generate a new Rails application?

Use the command rails new app_name --database=postgresql to create a new application with specified database configuration.[1]

7. What is the Gemfile in Rails and what is Bundler?

Gemfile declares project dependencies. Bundler manages gem installation with bundle install, ensuring consistent gem versions across environments.[3]

8. What are Rails environments?

Rails has three main environments: development, test, and production, configured via config/environments files and controlled by the RAILS_ENV variable.[3]

9. Explain the difference between nil and false in Ruby.

nil represents the absence of a value, while false is a boolean value indicating falsiness. Both are falsy but distinct objects.[5]

10. What is scaffolding in Rails?

Scaffolding generates a complete set of model, controller, views, and routes for CRUD operations using rails generate scaffold ModelName, providing a quick prototype.[5]

Intermediate Ruby on Rails Interview Questions

11. What are Active Record associations? Name the main types.

Active Record associations define relationships between models: belongs_to, has_one, has_many, has_many :through, and has_and_belongs_to_many. They use foreign keys automatically.[3]

12. How do you add validations to a Rails model?

Validations ensure data integrity using methods like validates :name, presence: true or validates :email, uniqueness: true in the model class.[3]

class User < ApplicationRecord
  validates :email, presence: true, uniqueness: true
  validates :age, numericality: { greater_than: 18 }
end

13. Explain Rails callbacks with examples.

Callbacks execute code at specific lifecycle points like before_save, after_create, or before_destroy. Example: before_save :set_slug to generate slugs automatically.[1]

14. What is RESTful routing in Rails?

RESTful routes map HTTP methods to controller actions: GET /articles (index), POST /articles (create), GET /articles/:id (show), etc., following standard conventions.[2]

resources :articles
# Generates: GET /articles, POST /articles, GET /articles/:id, etc.

15. How do you check defined routes in a Rails app?

Run rails routes in the terminal to list all routes with their paths, controllers, and actions.[3]

16. What are Rails helpers?

Helpers are methods in app/helpers that simplify view code, like link_to or custom methods for formatting dates and generating HTML.[3]

17. Explain Active Record transactions.

Transactions ensure multiple operations succeed or fail together using ActiveRecord::Base.transaction. If any operation fails, all changes roll back.[3]

ActiveRecord::Base.transaction do
  user.save!
  post.save!
end

18. How do you create a controller in Rails?

Use rails generate controller Articles index show to create ArticlesController with specified actions, views, and routes.[5]

19. What are Rails partials and when do you use them?

Partials (_form.html.erb) are reusable view templates rendered with <%= render “partial” %>. They promote DRY code for repeated UI elements.[2]

20. What is an enum in Ruby on Rails?

Enums map symbolic names to integers in the database: enum status: { active: 0, inactive: 1 }, allowing queries like user.active?.[5]

Advanced Ruby on Rails Interview Questions

21. What are Rails concerns and how do you use them?

Concerns extract reusable code into modules in app/models/concerns. Include them with include ConcernName in models or controllers.[1]

# app/models/concerns/authentication.rb
module Authentication
  extend ActiveSupport::Concern
  included do
    before_action :authenticate_user!
  end
end

22. Explain caching strategies in Ruby on Rails.

Rails supports page, action, fragment, and low-level caching. Use cache helper in views or Rails.cache.fetch for performance optimization.[2]

23. How does Rails handle internationalization (i18n)?

i18n uses YAML files in config/locales with keys like en: hello: "Hello World". Set locale with I18n.locale = :en.[3]

24. What is ActiveSupport and what utilities does it provide?

ActiveSupport extends Ruby with utilities like blank?, present?, time extensions, and string inflections for Rails applications.[3]

25. How do you implement file uploads in Rails?

Use gems like Active Storage or CarrierWave. Active Storage configuration: rails active_storage:install and has_one_attached :avatar.[3]

26. What are Rails engines?

Engines are embeddable mini-Rails apps providing reusable functionality. Mountable engines can be included in host apps via mount Engine::Engine => "/path".[3]

27. Explain the difference between load and require in Ruby.

require loads a file once and prevents reloading. load executes a file every time it’s called, useful for development reloading.[4]

28. How do you optimize N+1 queries in Rails?

Use includes, eager_load, or preload to fetch associations in single queries: Article.includes(:comments).all.[1]

29. What security tools does Rails provide?

Rails includes protection against SQL injection, XSS, CSRF via strong parameters, and gems like Brakeman for security scanning.[3]

30. Scenario: At Zoho, you’re building a multi-tenant SaaS app. How would you scope data to tenants using Rails?

Implement a current_tenant method in ApplicationController and use default_scope { where(tenant_id: Tenant.current.id) } in models for tenant isolation.[1]

## Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *