10 Ruby on Rails interview
questions and answers

Looking for Ruby on Rails freelancers to build your team?
Check out Upwork's top Ruby on Rails Developers

Get Started
  • Q

    What are some advantages of using Ruby on Rails?

    A

    Famed coder Yukihiro “Matz” Matsumoto designed Ruby to make programmers “happy”—Rails affords you all the advantages of Ruby, including simple syntax, an extensive library, and a quickly growing community. Here are some advantages to look for in the developer’s answer.

    • Programmer Productivity: The Ruby framework isn’t called “Rails” for no reason—testimonies abound around the web on how Rails can quickly carry an app from conception, through development, and into production in record speed.
    • Built-In Testing: Rails enables developers to use supporting code called harnesses and fixtures to quickly draft simple extendable automated tests.
    • Open-Source: Rails is open-source and 100% free, and its compatibility with Linux means there are many open-source options available when constructing your solution stack.
    • Metaprogramming: The ability to write code that acts on code rather than data can be a huge advantage, and Rails makes it easy.
    See The AnswerHide The Answer
  • Q

    How does Ruby on Rails use the Model View Controller (MVC) framework?

    A

    Web development can often be divided into three separate but closely integrated subsystems:

    • Model (Active Record): The model handles all the data logic of the application. In Rails, this is handled by the Active Record library, which forms the bridge between the Ruby program code and the relational database.
    • View (Action View): The view is the part of the application that the end user sees. In Rails, this is implemented by the Action View library, which is based on Embedded Ruby (ERB) and determines how data will be presented.
    • Controller (Action Controller): The controller is like the data broker of an application, handling the logic that allows the model and view to communicate with one another. This is called the Action Controller in Rails.
    See The AnswerHide The Answer
  • Q

    What is the purpose of the rakefile available in the demo directory in Ruby?

    A

    The purpose of this simple question is to make sure a developer is familiar with a test-driven development. A beginner may not have dealt with this file yet. The rakefile is similar to the makefile in Unix, and assists with packaging and testing Rails code. It’s used by the rake utility, which ships natively with the Ruby installation.

    See The AnswerHide The Answer
  • Q

    Explain the role of the subdirectories app/controllers and app/helpers in Rails.

    A

    The app/controllers subdirectory holds all the controller classes for the app. Controllers handle web requests from the user. The app/helpers subdirectory holds helper classes, which are used to assist the model, view, and controller classes. By keeping helper classes in a separate subdirectory, the model, view, and controller classes can remain lean and uncluttered.

    See The AnswerHide The Answer
  • Q

    How does Rails implement AJAX?

    A

    Asynchronous JavaScript and XML (AJAX) is a suite of technologies used to retrieve data for a webpage without having to refresh the page itself. This is how modern websites are able to cultivate a “desktop-like” user experience. The Rails method of implementing AJAX operations is short and simple.

    • First, a trigger is fired. The trigger can be something as simple as a user clicking on a call to action.
    • Next, the web client uses JavaScript to send data via an XMLHttpRequest from the trigger to an action handler on the server.
    • On the server-side, a Rails controller action receives the data and returns the corresponding HTML fragment to the client.
    • The client receives the fragment and updates the view accordingly.
    See The AnswerHide The Answer
  • Q

    What is a Rails Migration? Write up a short example of a simple Rails Migration with a table called customers, a string column called name, and a text column called description.

    A

    Initiating the command c:rubyapplication>ruby script/generate migration table_name will create a Rails Migration. A Rails Migration can be used to create, drop, or remove tables and columns. A potential solution is provided below.

    class CreateCustomers < ActiveRecord::Migration
      def up
        create_table :customers do |t|
          t.string :name
          t.text :description
     
          t.timestamps
        end
      end
     
      def down
        drop_table :customers
      end
    end
    See The AnswerHide The Answer
  • Q

    What is the purpose of the resources method in the code snippet below?

    A
    resources :posts do
      member do
          get ‘messages’
      end
      collection do
          post ‘bulk_upload'
      end
    end

    Defining routes with the “resources” method automatically generates routes for the seven standard RESTful actions:

    • GET /messages
    • POST /messages
    • GET /messages/new
    • GET /messages/:id/edit
    • GET /messages/:id
    • PATCH/PUT /messages/:id
    • DELETE /messages/:id
    See The AnswerHide The Answer
  • Q

    Can you explain the difference between ActiveSupport’s “HashWithIndifferentAccess” and Ruby’s “Hash”?

    A

    The “HashWithIndifferentAccess” class will treat symbol keys and string keys as equivalent while the “Hash” class in Ruby will use the stricter = = comparison on keys—an equivalent string key will not retrieve the value for a given symbol key.

    See The AnswerHide The Answer
  • Q

    Explain the role of garbage collection in Ruby on Rails.

    A

    Garbage collection frees up memory for other processes by removing pointer programs and inaccessible objects left behind after a program has executed. This frees the programmer from having to track objects created dynamically during runtime.

    See The AnswerHide The Answer
  • Q

    You’re setting up the routes for a website that displays information on different genres of music. Using the URL path /music/ as an example, create a route that uses a single controller action for each genre of music with the actual music genre passed into the controller action as a parameter. The valid music genres are as follows: classical, rock, house, country, hip_hop, and rnb. All other genres should generate a 404 status code.

    A

    You will likely receive a variety of potential answers for an open-ended coding question like this, but here are two of the most straightforward approaches to creating a route. The most straightforward solution would be to create a simple route that specifies the controller action to call and passes the genre of music as a parameter. If the genre parameter is not included in the list of valid genres, the action will raise an ActionController::RoutingError and redirect to a 404 page.

    get 'music/:genre' => 'music#genre'

    Alternatively, a better solution would be to use resource routing which comes with the added benefit of URL generation helpers. You can use the constraints option to verify the validity of a route by checking the parameter against a list of genres. This method requires that the parameter name for the genre be passed as :id and would look something like this:

    genres = %w|classical rock house country hip_hop rnb|
    resource :music, only: [:show], constraints: {id: Regexp.new(genres.join('|'))}
    See The AnswerHide The Answer

Submit a Ruby on Rails interview question

Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Upwork.

{{ vm.feedbackMessage }}
{{ vm.feedbackMessage }}

At a glance: Ruby on Rails Developers

The web development framework Ruby on Rails allows programmers to use the Ruby programming language to build applications for the web. A Ruby on Rails developer can rapidly prototype, test, and build powerful web applications capable of handling large volumes of traffic. Whether you’re looking to build a scalable website for your startup or need to revamp the server-side of an existing web app, these interview questions can help you find the right Ruby on Rails developer for your needs.

Bring your idea to life with a Ruby on Rails Developer

Get Started