Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby On Rails Interview Questions and Answers

Ques 31. What is the purpose of Layouts in Ruby on Rails?

Layouts are partial ruby/html files that are used to render the content pages. 

There are placed in the folder: app/views/layouts

Items that you would typically put in this folder are things like headers/footers, navigation elements, etc.

Here’s a sample layout file: /app/views/layout/application.html.erb

<html lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Learning System | <%= @page_title || 'Admin Area' %></title>
    <meta name="author" content="Anil Punjabi">
    <%= stylesheet_link_tag('public', 'admin', :media => 'all') %>
    <%= javascript_include_tag('application') %>
    <div id="header">
      <h1>Learning System</h1>
    <div id="main">
      <% if !flash[:notice].blank? %>
      <div class="notice">
        <%= flash[:notice] %>
      <% end %>
      <div id="content">
        <%= yield %>
    <div id="footer">
      <p id="copyright">© / Anil Punjabi</p>
Say you are trying to access the page as shown below:
Then the contents of the index.html.erb would be placed above in the section shown under <% yield %> above and sent back to the user.

Is it helpful? Add Comment View Comments


That is the purpose of layouts. You embed partial views inside the file /app/views/layout/application.html.erb and then whenever you render any page this layout is merged with it.

Is it helpful? Add Comment View Comments

Ques 33. What is Rake in Ruby on Rails?

Rake is a popular ruby gem that makes the job of running tasks simpler. 

Rake is most often used for DB tasks, but it can be used for m

The common DB commands are:

rake db:migrate
rake db:reset

You can use cron to schedule rake tasks. 

Sometimes you would create a dataloader.rake file and put it in the lib/tasks folder so that it can be used to populate the database on startup.

Is it helpful? Add Comment View Comments

Ques 34. What is Capistrano?

Capistrano is a popular deployment tool — it allows developers to push code from their desktop to the servers. 

Is it helpful? Add Comment View Comments

Ques 35. What is Eager Loading in Ruby on Rails?

Eager loading is a great optimization strategy to reduce the number of queries that are made against the DB.

Say you are finding 10 employees and then you are looking for their post codes. Then your query would appear something like this:

clients = Client.limit(10)
clients.each do |client|
  puts client.address.postcode

This may seem fine at first look but really this implementation leaves much to be desired. It makes 11 DB calls just to get the results.

Now you can optimize this query by making a slight change in the request like this:

clients = Client.includes(:address).limit(10)
clients.each do |client|
  puts client.address.postcode

This new request makes two SQL calls like this:

SELECT * FROM clients LIMIT 10
SELECT addresses.* FROM addresses
    WHERE (addresses.client_id IN (1,2,3,4,5,6,7,8,9,10))
So, as you can see it really loads a lot more upfront and therefore it is called eager loading.

Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2023 WithoutBook