Without Book Interview Questions and Answers | Online Test | Moct Test
Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Institute Training Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby On Rails Interview Questions and Answers

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

Ans.

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
end

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
end 

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
Ques 57. How does Validation works in Ruby on Rails?
Ans.

Validation means checking to see if data  is good before it is stored in the database.

During signups and other such user input cases you want to check and be sure that the data is validated. In the past developers would often put this type of validation logic as triggers in the database.  

In an MVC architecture one can do validations at each level. 

You can do validations in the controllers but it is usually a good idea to keep your controllers skinny.

Views suffer from the javascript limitation because javascript can be disabled on the client side so they are not completely reliable.

The best way to manage validation is to put it in the model code. This model code is really the closest as you can be to the database and works very well for Rails applications.

Here are a few validation examples:

class Person < ActiveRecord::Base
  validates :name, :length => { :minimum => 2 }
  validates :points, :numericality => { :only_integer => true }  # only integer
  validates :age,  :numericality => { :greater_than => 18 } # greater than 18
  validates :email, :uniqueness => true
  validates :email, :confirmation => true  # this is to validate that the two email fields are identical
  validates :email_confirmation, :presence => true # this is to validate that the email confirmation field is not nil

In your view template you may use something like this:

 <%= text_field :person, :email %> <%= text_field :person, :email_confirmation %>
Is it helpful? Add Comment View Comments
Ques 58. How can you add custom validation on your model in Ruby on Rails?
Ans.

Now custom validations takes it to the next step.

Say you want to confirm that the data meets certain criteria 

Is it helpful? Add Comment View Comments
Ques 59. What is Flash in Ruby on Rails?
Ans.

Flash  is simply a way to pass some value to the next action. 

Anything you place in the flash will be exposed to the very next action and then cleared out.

Here’s an example:

def destroy

section = Section.find(params[:id])      
section.destroy      
FLASH[:NOTICE] = "SECTION DESTROYED."     
redirect_to(:action => 'list', :page_id => @page.id)    

end

Then wherever you want to use the flash you can write this code. I often put this snippet in the application.html.erb file, somewhere towards the top:

   <% if !flash[:notice].blank? %>         
<div class="notice">  
 <%= flash[:notice] %>
Is it helpful? Add Comment View Comments
Ques 60. HOW CAN YOU INSTALL THE MISSING GEMS THAT ARE REQUIRED BY YOUR APPLICATION IN THE SIMPLEST WAY in Ruby on Rails?
Ans.
bundle install
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2016 WithoutBook