Posted in

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

Prepare for your Ruby on Rails interview with these 30 essential questions covering basic, intermediate, and advanced topics. This guide helps freshers, developers with 1-3 years experience, and senior engineers with 3-6 years of experience master Rails concepts, Active Record, MVC architecture, and more.

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 MVC 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 Rails MVC, Models handle data and business logic, Views render the user interface, and Controllers manage communication between models and views by processing requests and returning responses.[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 MigrationName, then run rails db:migrate.[5]

4. What is the difference between rails server and rails console?

rails server starts the web server (usually Puma or WEBrick) to handle HTTP requests. rails console opens an interactive Ruby shell with Rails environment loaded for testing code.[1]

5. Explain Rails naming conventions for models, controllers, and tables.

Models are singular CamelCase (User), controllers are plural CamelCase (UsersController), tables are plural snake_case (users), and foreign keys are singular_id (user_id).[5]

6. What is Active Record in Ruby on Rails?

Active Record is Rails’ Object-Relational Mapping (ORM) library that maps database tables to Ruby classes, handles CRUD operations, and provides associations like belongs_to and has_many.[1]

7. How do you generate a new Rails application?

Use rails new app_name --database=postgresql to create a new Rails app with specified database. Navigate to the directory and run rails server to start.[1]

8. What is the Gemfile and how do you install gems?

Gemfile lists project dependencies. Run bundle install to install all gems specified in the Gemfile and create Gemfile.lock for exact versions.[3]

9. Explain the difference between find, find_by, and find_by! in Active Record.

find(id) retrieves by primary key or raises error if not found. find_by(key: value) returns nil if not found. find_by!(key: value) raises ActiveRecord::RecordNotFound if not found.[3]

10. What are Rails helpers?

Helpers are Ruby methods in app/helpers that simplify view code, like formatting dates or generating links. They promote DRY principle in views.[3]

Intermediate Ruby on Rails Interview Questions

11. What are Rails callbacks and name some common ones for saving/updating objects.

Rails callbacks execute code at specific lifecycle points. Common ones include before_save, after_validation, before_update, after_commit.[1]

12. Explain RESTful routing in Rails with an example.

RESTful routes map HTTP methods to controller actions: GET /articles → index, POST /articles → create, GET /articles/:id → show, etc. Use resources :articles in routes.rb.[2]

resources :articles do
  resources :comments
end

13. What are Active Record validations and how do you implement them?

Validations ensure data integrity. Examples: validates :email, presence: true, uniqueness: true or validates :age, numericality: { greater_than: 18 }.[3]

14. How do you handle strong parameters in Rails controllers?

Strong parameters whitelist permitted attributes: params.require(:user).permit(:name, :email, :role) prevents mass assignment vulnerabilities.[1]

15. What are Rails partials and when would you use them at Atlassian?

Partials (_form.html.erb) are reusable view components. At Atlassian, use partials for shared UI elements like comment forms across Jira and Confluence projects.[2]

16. Explain Rails associations with examples.

Associations define model relationships: has_many :posts in User, belongs_to :user in Post, has_many :through for join tables.[3]

17. What is the difference between save and save!?

save returns true/false based on validation success. save! raises ActiveRecord::RecordInvalid exception if validation fails.[3]

18. How do you use Rails transactions?

Transactions ensure multiple operations succeed or fail together: ActiveRecord::Base.transaction do user.save! post.save! end. Rolls back on any failure.[3]

19. What are Rails scopes and how do you define them?

Scopes are custom finders: scope :active, -> { where(active: true) }. Call as User.active for reusable queries.[1]

20. Explain how to implement authentication in Rails at Paytm.

At Paytm, use has_secure_password in User model with bcrypt: validates :password, presence: true and sessions controller for login/logout.[1]

Advanced Ruby on Rails Interview Questions

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

Concerns modularize code across models/controllers using modules. Create in app/models/concerns, then include ConcernName.[1]

module Authentication
  extend ActiveSupport::Concern
  included do
    before_action :authenticate_user!
  end
end

22. How does caching work in Ruby on Rails and what strategies exist?

Rails caching stores computed results: page caching, action caching, fragment caching. Use Rails.cache.fetch('key') { expensive_operation }.[2]

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

require loads a file once. load reloads every time, useful in development. require uses $LOAD_PATH.[5]

24. What is an Active Record engine and when would Salesforce use one?

Engines are mini-Rails apps for reusable functionality. Salesforce uses engines for modular billing or authentication features across microservices.[3]

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

Use includes: User.includes(:posts).all eager loads associations. Check with bullet gem or rails panel.[1]

26. What are Ruby instance, class, and global variables?

Instance (@var), class (@@var), global ($var). Instance scoped to object, class to class instances, global application-wide.[5]

27. Explain super vs super() in Ruby inheritance.

super passes all arguments to parent. super() passes no arguments. Mismatched arity causes ArgumentError.[4]

28. How would you implement internationalization (i18n) for a Swiggy-like app?

Use config/locales/*.yml files. Set I18n.locale = :en. Translate with t('hello') or <%= t('.title') %>.[3]

29. What is ActiveSupport and name some useful methods.

ActiveSupport extends Ruby classes. Useful methods: blank?, present?, try(), time extensions like 1.day.ago.[3]

30. How do you secure file uploads in Rails for an Oracle SaaS platform?

At Oracle, use Active Storage with has_one_attached :avatar, validate content_type, and store on secure cloud (S3). Limit file size with validators.[3]

Leave a Reply

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