Ruby On Rails Easy Interview Questions

Questions to ask a Ruby on Rails Developer

Ruby on Rails is unique in its ability to create a complete, working web application in approximately 5 minutes. A good Ruby on Rails interview should be an interactive coding session in which the candidate demonstrates their mastery of rapid iteration programming.

  1. Kick things off by asking them to Create a basic Rails App with nested routes .
  2. Familiarity with Javascript/AJAX is essential for modern Rails development. Ask the candidate to expand on their sample application and Load content via AJAX using Rails UJS.
  3. Designing a good data model is essential. Even more essential is designing a data model that fits well within the Rails framework. Build again on the sample app, by demonstrating their mastery of Rails Polymorphic Resources.
  4. Verify that they have a solid understanding of Rails Caching and especially Rails Caching and Slightly Dynamic Content.
  5. Ensure that they won't leave any gaping security holes in your application by asking about Rails Security: Mass Assignment.

RailsOnRails is unique in its ability to create a complete, working web application in approximately 5 minutes. A solid RoR engineer should be able to demonstrate this.

Create a new RoR application with two models: Blog and Comment. Blog should have two string properties: title and body. Comment has one string property: body. A Comment belongs to a Blog.

Configure routes such that Blogs are shown from path /blogs/:id and the comments for a particular blog are created at /blogs/:blog_id/comments/new and viewed at /blogs/:blog_id/comments.

Create a new RoR application with two models: Blog and Comment. Blog should have two string properties: title and body. Comment has one string property: body. A Comment belongs to a Blog.

The route /blog/:id should show the title and body properties of the blog. Below the body of the blog, add a link that says "Show Comments". When this link is clicked, load and display the comments for the blog via AJAX.

Suppose you have a Rails User model as follows:

class User < ActiveRecord::Base
   # Properties of this model are string:name and boolean:is_admin
end

The is_admin property is used to denote whether or not a user should be granted administrative privileges. Only users who are admins should be able to grant admin privileges to users.

Suppose you expose a form in your application in which any user may edit their name. For example

<%= form_for @user %>
   <%= f.label :given_name %>  
   <%= f.text_field :given_name %>
   <%= f.submit "Update"  %>
<% end %>

Explain how you've just opened up a security hole in your application. How can you fix this?

Write a helper method that takes a DateTime object date as it's single parameter.

  1. If the date is less then two days ago, show the distance of time in words. (e.g. "about 4 hours ago").
  2. If the date is more then two days ago, show the actual date. (e.g. Jan 12, 2012)

E.g. fill in the guts of the following method

def distance_of_time_in_words_or_absolute(date)
   # Your code here
end

Consider a Rails application with the following two models: Book and Video. Book has properties title and summary. Video has properties title and studio. Suppose that when both of these models are displayed in a the application, a user may add a Comment (with single property body). (E.g. both Books and Videos have many Comments).

Create the appropriate migrations and models (including relationships).

Now, suppose on your home page you want to show a list of all recent Comments, sorted by created_at. The list should show the body of the Comment as well as the type and title of the object about which the comment was made.

Write the ActiveRecord query necessary to display this list of recent Comments. Explain what the N + 1 query problem is, and how you've avoided it in your solution.

Suppose you have the following set of models and relationships

class Author < ActiveRecord::Base
   has_many :blogs
   # Properties:  name
end

class Blog << ActiveRecord::Base
  belongs_to :author
  has_one :image
  # Properties:  title, body
end

class Image << ActiveRecord::Base
  belongs_to :blog
  # Properties:  image_file_path
end

Suppose you want to create a table that shows all Blogs. The columns of the table are the blog title, the author name and the blog image.

Write an ActiveRecord query that will pull the list of @blogs necessary to display this table. Can you do this such that under the covers no more then 3 SQL statements are generated? How about no more then 1?

Create a Rails application with two models: User (properties: string:name) and Address (properties: string:street, string:city). A User has one Address.

Create a view / controller in which a new User and their Address may be created in a single form. Make all properties for both models are required in the form.

Suppose you have a User model with properties string:name, integer:age, string:occupation.

Write validations on the model such that

  1. Name is required
  2. Age is optional, but must be numeric, and not less then 5
  3. Occupation is required if the age is greater then 21

Suppose you have two models User and Address. A User has at most one address. User has property datetime:last_login_at. Address has properties string:city and string:state.

Write an ActiveRecord query to return an array of Users who live in California, ordered by the last_login_at.

Write an ActiveRecord query to return an array of Users who live in California or have no listed address.