Rails,Ruby on Rails Interview Questions and Answers -Ruby on Rails interview questions and answers for freshers and experienced
Ruby on Rails (“Rails”) is a web development framework written in the Ruby programming language. Since its debut in 2004, Ruby on Rails has rapidly become one of the most powerful and popular tools for building dynamic web applications. Rails is a server-side programming web application framework written in Ruby programming under the MIT License. Rails is a MVC framework, providing default structures for a database, a web service, and web pages. It encourages and facilitates the use of web standards such as JSON or XML for data transfer, and HTML, CSS and JavaScript for display and user interfacing. In addition to MVC, Rails emphasizes the use of other well-known software engineering patterns and paradigms, including convention over configuration (COC), don't repeat yourself (DRY), and the active record pattern.
First version Rails 1.0         December 13, 2005   Announcement.
Current version Rails 5.1.4    September 7, 2017      Released

What is Rails?
Rails is a web application development framework written in the Ruby programming language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It enables the programmer to easily create advanced database-driven websites using scaffolding and code generation, following convention over configuration, which means that if you stick to a certain set of conventions, a lot of features will work right out of the box with very little code. Rails can run on most Web servers that support CGI. The framework also supports MySQL, PostgreSQL, SQLite, SQL Server, DB2 and Oracle. Rails is also an MVC (model, view, controller) framework where all layers are provided by Rails, as opposed to reliance on other, additional frameworks to achieve full MVC support.
What is different about Rails?
Ruby on Rails is different than other frameworks.
  • Everything is an object
  • Independent of the web server
  • Meta programming techniques are used to write programs. Ruby is one of the best language supports for Meta programming and Rails also uses this support as well for code generation.
  • Convention over configuration: There are lots of development frameworks for java or .net frameworks; they are forced to write configuration codes in some pages. But Rails doesn't need much configuration.
  • Active Record: Rails introduces the Active Record framework service, which saves objects to the database. The Active Record shows data columns in a database and it automatically attaches to our domain using Meta programming.
  • Built-in testing Rails creates automated tests. Rails also gives supporting code called enhancing and error fixtures that make it easier to write the code and execute the code.
What are the major features in Rails 5.1?
  • System tests
  • Yarn Support
  • Optional Web pack support
  • JQuery no longer a default dependency
  • Encrypted secrets
  • Parameterized mailers
  • Direct & resolved routes
  • Unification of form for and form tag into form with
What are next rails 5.2.0?
In Rails 5.2.0 new features are:
  • Active Storage
  • New Framework
  • PostgreSQL features
  • Redis Cache Store
  • HTTP/2 Early Hints
  • CSP
  • New concept: Credentials
What are the benefits of using RoR (Ruby on Rails)?
  • Ruby syntax is ideal for those who prefer pattern-matching characters because it uses differing characters as analogues to keywords
  • It’s perfect for web application
  • Main selling point of Rails is the philosophy called convention over configuration (CoC) meaning it is a framework that has a structured layout with defaults (read: it’s easy to get up and running - sort of like a web app starter kit)
  • It’s built for speed and adaptation
  • Strong support community both online and offline
  • Save money and time
What is Ruby on Rails?
Ruby is a general purpose Programming language whereas Ruby on Rails is a web application framework implemented by Ruby. So they not only work in collaboration but Ruby on rails is completely dependent on Ruby.
Ruby: Ruby is a programming language that was created in the mid-1990s in Japan by Yukihiro Matsumoto. It is an object oriented programming language inspired by PERL and PYTHON. 
Rails: Rails is an open source web app framework, written in Ruby that appeared in 2005. It is a framework used for building web application.

What is class libraries in Ruby?
Class libraries in Ruby consist of a variety of domains, such as data types, thread programming, various domains, etc.

Why does MVC make sense in Rails?
MVC allows for perfect separation of business logic from presentation logic, giving you a much cleaner application that is easier to maintain and, on top of that, a lot more fun to develop.

What is the scaffolding in Ruby on Rails?
Scaffolding is a quick way to produce some major pieces of an application. For auto generating a set of models, views and controllers for a new resource in a single operation, scaffolding is used. Scaffolding is a technique supported by MVC frameworks in which programmers can specify how application database may be used.

Mention what is the naming convention in Rails?
Variables: For declaring Variables, all letters are lowercase, and words are separated by underscores
Class and Module: Modules and Classes uses MixedCase and have no underscore; each word starts with a uppercase letter
Database Table: The database table name should have lowercase letters and underscore between words, and all table names should be in the plural form for example invoice_itemsModel: It is represented by unbroken Mixed Case and always have singular with the table name
Controller: Controller class names are represented in plural form, such that Orders Controller would be the controller for the order table.

What is “Yield” in Ruby on Rails?
A Ruby method that receives a code block invokes it by calling it with the “Yield”.

What is ORM (Object-Relationship-Model) in Rails?
ORM or Object Relationship Model in Rails indicate that your classes are mapped to the table in the database, and objects are directly mapped to the rows in the table.

what the difference is between false and nil in Ruby?
In Ruby False indicates a Boolean datatype, while Nil is not a data type, it have an object_id 4.

What is the role of sub-directory app/controllers and app/helpers?
App/controllers: A web request from the user is handled by the Controller. The controller sub-directory is where Rails looks to find controller classes
App/helpers: The helper’s sub-directory holds any helper classes used to assist the view, model and controller classes.

What is the difference between String and Symbol?
They both act in the same way only they differ in their behaviors which are opposite to each other. The difference lies in the object_id, memory and process tune when they are used together. Symbol belongs to the category of immutable objects whereas Strings are considered as mutable objects.

Explain how Symbol is different from variables in ROR?
Symbol is different from variables in following aspects
  • It is more like a string than variable
  • In Ruby string is mutable but a Symbol is immutable
  • Only one copy of the symbol requires to be created
  • Symbols are often used as the corresponding to enums in Ruby
What is Rails Active Record in Ruby on Rails?
Rails active record is the Object/Relational Mapping (ORM) layer supplied with Rails. It follows the standard ORM model as
  • Table map to classes
  • Rows map to objects
  • Columns map to object attributes
Explain how Rails implements Ajax?
Ajax powered web page retrieves the web page from the server which is new or changed unlike other web-page where you have to refresh the page to get the latest information.
Rails triggers an Ajax Operation in following ways
Some trigger fires: The trigger could be a user clicking on a link or button, the users inducing changes to the data in the field or on a form
Web client calls the server: A Java-script method, XMLHttpRequest, sends data linked with the trigger to an action handler on the server. The data might be the ID of a checkbox, the whole form or the text in the entry field
Server does process: The server side action handler does something with the data and retrieves an HTML fragment to the web client
Client receives the response: The client side JavaScript, which Rails generates automatically, receives the HTML fragment and uses it to update a particular part of the current

Mention how you can create a controller for subject?
To create a controller for subject you can use the following command
C:\ruby\library> ruby script/generate controller subject

Mention what is Rails Migration?
Rails Migration enables Ruby to make changes to the database schema, making it possible to use a version control system to leave things synchronized with the actual code.

List out what can Rails Migration do?
Rails Migration can do following things
  • Create table
  • Drop table
  • Rename table
  • Add column
  • Rename column
  • Change column
  • Remove column and so on
What is the command to create a migration?
To create migration command includes
C:\ruby\application>ruby script/generate migration table_name

 When self.up and self.down method is used in Ruby on Rails?
When migrating to a new version, self.up method is used while self.downmethod is used to roll back my changes if needed.

What is request.xhr in Ruby on Rails?
A request.xhr tells the controller that the new Ajax request has come, it always return TRUE or FALSE

What is the role of Rails Controller?
The Rails controller is the logical center of the application. It faciliates the interaction between the users, views, and the model. It also performs other activities like
  • It is capable of routing external requests to internal actions. It handles URL extremely well
  • It regulates helper modules, which extend the capabilities of the view templates without bulking of their code
  • It regulates sessions; that gives users the impression of an ongoing interaction with our applications
What is the difference between Active support’s “HashWithIndifferent” and Ruby’s “Hash”?
The Hash class in Ruby’s core library returns value by using a standard “= =”comparison on the keys. It means that the value stored for a symbol key cannot be retrieved using the equivalent string. While theHashWithIndifferentAccess treats Symbol keys and String keys as equivalent.

What is Cross-Site Request Forgery (CSRF) and how Rails is protected against it?
CSRF is a form of attack where hacker submits a page request on your behalf to a different website, causing damage or revealing your sensitive data. To protect from CSRF attacks, you have to add“protect_from_forgery” to your ApplicationController. This will cause Rails to require a CSRF token to process the request. CSRF token is given as a hidden field in every form created using Rails form builders.

What is Mixin in Rails?
Mixin in Ruby offers an alternative to multiple inheritances, using mixin modules can be imported inside other class.

What is active record in Ruby on rails?
Active Record is the M in MVC- the model. Active Records provide an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. ActiveRecord is a Ruby library for working with Relational SQL Databases like MySQL and Postgres. It provides an Object Relational Mapping (ORM) with these core features:
  • A DSL for easily constructing SQL queries in Ruby
  • A single Ruby object maps to a database table
  • Columns are accessed by methods, and are inferred from the database schema
  • Methods for create, read, update, and delete (CRUD) are defined
Explain how you can run Rails application without creating databases?
You can execute your application by uncommenting the line in environment.rb
path=> rootpath conf/environment.rb

What is the difference between the Observers and Callbacks in Ruby on Rails?
Rails Observers: Observers is same as Callback, but it is used when method is not directly associated to object lifecycle. Also, the observer lives longer, and it can be detached or attached at any time. For example, displaying values from a model in the UI and updating model from user input.
Rails Callback: Callbacks are methods, which can be called at certain moments of an object’s life cycle for example it can be called when an object is validated, created, updated, deleted, A call back is short lived. For example, running a thread and giving a call-back that is called when thread terminates

What is rake in Rails?
Rake is a Ruby Make; it is a Ruby utility that substitutes the Unix utility ‘make’, and uses a ‘Rakefile’ and ‘.rake files’ to build up a list of tasks. In Rails, Rake is used for normal administration tasks like migrating the database through scripts, loading a schema into the database, etc.

Explain how you can list all routes for an application?
To list out all routes for an application you can write rake routes in the terminal.

What is sweeper in Rails?
Sweepers are responsible for expiring or terminating caches when model object changes.
Mention the log that has to be seen to report errors in Ruby Rails?
Rails will report errors from Apache in the log/Apache.log and errors from the Ruby code in log/development.log.

Explain what is the difference between Dynamic and Static Scaffolding?
Dynamic Scaffolding:       
  • It automatically creates the entire content and user interface at runtime
  • It enables to generation of new, delete, edit methods for the use in application
  • It does not need a database to be synchronized       
Static Scaffolding:
  • It requires manual entry in the command to create the data with their fields
  • It does not require any such generation to take place
  • It requires the database to be migrated
Mention what is the function of garbage collection in Ruby on Rails?
The functions of garbage collection in Ruby on Rails includes
  • It enables the removal of the pointer values which is left behind when the execution of the program ends
  • It frees the programmer from tracking the object that is being created dynamically on runtime
  • It gives the advantage of removing the inaccessible objects from the memory, and allows other processes to use the memory
What is the difference between redirect and render in Ruby on Rails?
Redirect: Redirect is a method that is used to issue the error message in case the page is not issued or found to the browser. It tells browser to process and issue a new request.
Render: Render is a method used to make the content. Render only works when the controller is being set up properly with the variables that require to be rendered.

What is Session and Cookies in Ruby on Rails?
Session: Session are used to store user information on the server side.
Cookies: Cookies are used to store information on the browser side or we can say client side
Session: say session [: user] = “John” it remains when the browser is not closed

What is the purpose of RJs in Rails?
RJs is a template that produces JavaScript which is run in an eval block by the browser in response to an AJAX request. It is sometimes used to define the JavaScript, Prototype and helpers provided by Rails.

Explain what is Polymorphic Association in Ruby on Rails?
Polymorphic Association allows an ActiveRecord object to be connected with Multiple ActiveRecord objects. A perfect example of Polymorphic Association is a social site where users can comment on anywhere whether it is a videos, photos, link, status updates etc. It would be not feasible if you have to create an individual comment like photos_comments, videos_comment and so on.

How to use Nested routes in ROR?
The easiest way to create a nested route, is to use the: has_many keyword like that:
# /config/routes.rb
map.resources :projects, :has_many => :tasks
# and the correspondent task resource
map.resources :tasks

What is the difference between a plugin and a gem?
Gem: A gem is just ruby code. It is installed on a machine and it’s available for all ruby applications running on that machine. Rails, rake, json, rspec — are all examples of gems.
Plugin: Plugin is also ruby code but it is installed in the application folder and only available for that specific application. Sitemap-generator, etc.
In general, since Rails works well with gems you will find that you would be mostly integrating with gem files and not plugins in general. Most developers release their libraries as gems.

Mention what are the limits of Ruby on Rails?
Ruby on Rails has been designed for creating a CRUD web application using MVC. This might make Rails not useful for other programmers. Some of the features that Rails does not support include
  • Foreign key in databases
  • Linking to multiple data-base at once
  • Soap web services
  • Connection to multiple data-base servers at once
What is difference between -%> and %> in ROR?
The extra dash makes ERB not output the newline after the closing tag. There’s no difference in your example, but if you have something like this:
<% if true -%>
<% end -%>
It’ll produce:  Hi
and not this:
Request to Download PDF


Vestibulum bibendum felis sit amet dolor auctor molestie. In dignissim eget nibh id dapibus. Fusce et suscipit orci. Aliquam sit amet urna lorem. Duis eu imperdiet nunc, non imperdiet libero.

Post A Comment: