Ruby on Rails Interview Questions and Answers
Rails interviews test convention-over-configuration thinking: how a request flows through routes, controllers, models and views, how ActiveRecord maps tables and associations, validations, scopes and migrations, and when callbacks help or hurt.
Performance and production topics follow: the N+1 query problem and eager loading, database indexes, caching layers, background jobs, security defaults (strong parameters, CSRF, SQL injection) and test strategy. Senior rounds ask how you would structure a large Rails app and keep models thin.
Questions
- What is MVC in Rails, and how does a request flow through it?
- Walk me through the structure of a Rails application. What lives where, and why?
- What does "Convention over Configuration" mean in Rails? Give examples and trade-offs.
- How does routing work in Rails? Explain the main routing features you use.
- What are RESTful resources in Rails, and what 7 actions does `resources` generate?
- How do you model many-to-many relationships in Rails? Compare `has_many :through` and `has_and_belongs_to_many`.
- How do Rails database migrations work, and how do you write them safely?
- How do ActiveRecord validations work? Why aren't they enough on their own?
- What are ActiveRecord callbacks? When are they appropriate and when do they become a problem?
- How do `nil` and `false` work in Ruby? What is truthy/falsy, and what are `blank?`, `present?` and `&.`?
- What are destructive (bang) methods in Ruby? What are the pitfalls?
- What's the difference between `load`, `require`, `require_relative` and autoloading in Ruby/Rails?
- What are gems and Rails engines? How does Bundler manage dependencies, and when would you build an engine?
- What's the difference between `git stash apply` and `git stash pop`? How do you use stash safely?
- What does `git cherry-pick` do, and when is it appropriate (or risky) to use?
- What is the N+1 query problem in Rails? How do you detect and fix it?
- What is the difference between `joins` and `includes` in ActiveRecord? When do you use each?
- How does an INNER JOIN behave in ActiveRecord, and what surprises does it cause (missing rows, duplicates, counts)?
- How do you join nested associations in ActiveRecord, and how do you filter on them correctly?
- What's the difference between `select` and `pluck` in ActiveRecord? When should you use each?
- What is the difference between ActiveRecord `group` and Ruby's `group_by`?
- How do you use SQL aliases in ActiveRecord queries (computed columns, subqueries, self-joins) safely?
- What are ActiveRecord scopes? How do they differ from class methods, and what about `default_scope`?
- What is a counter cache in Rails? How does it work, and what are its limitations?
- How do database indexes work, and how do you decide which indexes to add in a Rails app?
- What is a composite (multi-column) index? How does column order matter?
- How do you read a query plan (`EXPLAIN` / `EXPLAIN ANALYZE`) to diagnose a slow query in Rails?
- How do database transactions work in Rails? Explain rollback behavior, nesting and common pitfalls.
- What is pessimistic locking in Rails? How do you use `lock`/`with_lock`, and what are the risks?
- What is optimistic locking in Rails and how does `lock_version` work?
- How does Rails support multiple databases (primary/replica, separate databases, sharding)?
- How do you use Redis as a cache store in Rails? What configuration and pitfalls matter?
- Why is Redis `FLUSHALL` dangerous in production, and how do you clear cache data safely?
- What is delegation in Rails (`delegate`, `delegate_missing_to`, Forwardable) and why use it?
- Callbacks vs service objects in Rails: where should business workflows live, and why?
- How do you implement observer/pub-sub event patterns in Rails (ActiveSupport::Notifications, event buses)?
- What is Active Job? How does it work with backends like Sidekiq, and what are the best practices?
- How do you schedule recurring (cron) jobs in a Rails application, and what problems must you avoid?
- Explain Ruby's object model and method lookup path.
- What is metaprogramming in Ruby? Show the main tools and when you should (and shouldn't) use it.
- What are singleton methods and singleton classes (eigenclass/metaclass)? How do class methods relate to them?
- How does `method_missing` work? How do you implement it correctly, and what are the alternatives?
- How do mixins work internally in Ruby? Explain `include`, `extend` and `prepend`.
- What is duck typing in Ruby? Give practical examples and explain its risks.
- What is Marshal in Ruby? When is it used, what can't it serialize, and why is it a security risk?
- Explain Ruby's threading model and the GVL (GIL). How does it affect Rails apps, and does it make code thread-safe?
- What are Fibers in Ruby, how do they differ from Threads, and what is the Fiber Scheduler?
- What is an Enumerator in Ruby, and how do lazy enumerators help with large or infinite data?
- How do blocks work with `yield`, and how is `yield` different from `&block` + `block.call`? (Include Proc vs lambda.)
- What is a DSL in Ruby, and how do you build one? Explain `instance_eval` vs `yield self`.
- What is Rake? How do you write, organize, test and safely run Rake tasks in a Rails app?
- How do you find and fix performance problems in Ruby code? Cover benchmarking, profiling tools and common optimizations.
- How does type checking work in Ruby with Sorbet and RBS? What are the trade-offs of adopting it?
- What Ruby-level security pitfalls should you know (eval, send, command injection, deserialization, regex), and how do you avoid them?
- What are the differences between MRI (CRuby), JRuby and TruffleRuby? When would you choose a non-MRI Ruby?
- How does immutability work in Ruby (`freeze`, frozen string literals, `dup` vs `clone`, `Data`)? Why does it matter?
- What features of Ruby make it so well suited for building frameworks like Rails?
- Is everything in Ruby an object? Are methods and blocks objects?
- Is Ruby statically or dynamically typed? Strongly or weakly typed?
- How do `next`, `break`, `redo` and `retry` work in Ruby? What is Ruby's equivalent of `continue`?
- Explain Ruby's method visibility: public, protected and private. Can you call a private method from outside the class?
- Why is `#each` preferred over `for ... in` loops in Ruby?
- What do Ruby's naming conventions signal (Capitalized identifiers, `?` predicates, `!` methods, sigils), and what happens with unassigned or reassigned constants?
- Explain the difference between `==`, `===`, `eql?` and `equal?` in Ruby, and when each is used.
- What's the difference between instance variables, class variables (`@@`) and class instance variables? Are class variables inherited?
- What is a module in Ruby, and how does it differ from a class?
- Why are symbols typically used as hash keys instead of strings? And why is it safe to use a (mutable) string as a hash key?
- What does it mean to coerce an object in Ruby? Explain `coerce` and implicit vs explicit conversions (`to_s` vs `to_str`).
- What are some key differences between Ruby and Python?
- What are the uses of the splat (`*`) and double splat (`**`) operators in Ruby?
- What are the different ways to invoke a method (and a lambda/proc) in Ruby?
- How do you specify default values for a Hash, and why use `Hash#fetch` instead of `Hash#[]`?
- What do `val1 = true and false` and `val2 = true && false` evaluate to? Explain `and`/`or` vs `&&`/`||`.
- What does `self` refer to in different contexts in Ruby?
- Why would you alias a method in Ruby? What's the difference between `alias` and `alias_method`?
- Which operator must be defined to include the `Comparable` module, and what do you get from it?
- Explain the Ruby idiom `a ||= b`. What does it expand to, and what are its pitfalls?
- What is the difference between `throw/catch` and `raise/rescue` in Ruby?
- When should you use `do...end` versus `{ }` for blocks? Is there a functional difference?
- How does block invocation differ from method invocation (argument handling, `return`)? What happens with mismatched block arguments, and when do you get a `LocalJumpError`?
Related
- SQL interview questions
- Microservices interview questions
- Node.js interview questions
- Practice coding problems online
Frequently asked questions
What is the N+1 query problem?
Loading a list of records and then querying once per record for an association. Fix it with eager loading (includes, preload or eager_load) so the associations load in one or two queries.
Are callbacks bad practice?
Not by themselves, but business logic hidden in callbacks is hard to test and reason about. Prefer explicit service objects for multi-step workflows and keep callbacks for simple, local concerns.
How do you keep Rails controllers thin?
Move business logic into models or service objects, use strong parameters and concerns for shared behaviour, and keep the controller to authentication, parameters and the response.