Top Ruby On Rails Interview Questions and answers
1. What Are Filters? And How Many Types Of Filters Are There In Ruby ?
Filters are methods that are run before, after or "around" a controller action.
Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application.
Filter can take one of three forms: method reference (symbol), external class, or inline method (proc).
after_filter append_after_filter append_around_filter append_before_filter around_filter before_filter filter_chain prepend_after_filter prepend_around_filter prepend_before_filter skip_after_filter skip_before_filter skip_filter
2. Finding By Sql ?
If you'd like to use your own SQL to find records in a table you can use find_by_sql. The find_by_sql method will return an array of objects even if the underlying query returns just a single record.
For example you could run this query:
Client.find_by_sql("SELECT * FROM clients
INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc")
find_by_sql provides you with a simple way of making custom calls to the database and retrieving instantiated objects.
3. Explain About Restful Architecture?
RESTful: REST stands for Representational State Transfer. REST is an architecture for designing both web applications and application programming interfaces (API’s), that’s uses HTTP.
RESTful interface means clean URLs, less code, CRUD interface. CRUD means Create-READ-UPDATE-DESTROY. In REST, they add 2 new verbs, i.e, PUT, DELETE.
4. What Is The Role Of Mvc Architecture In Ruby On Rails?
MVC (Model-View-Controller) is the architecture that provides flexibility and scalability of the applications.
It is almost having the same concept in any other language like PHP, Perl or Python. It is one of the major used architecture involved today due to its simplicity.
Controller is the main part in this kind of architecture where it handles the request that is coming from another controller.
Controller contacts the view and passes on the request to the view and it also interacts with the model to define the type of request.
Model is responsible for interacting with the database and provides the responses to the controller.
Controller takes the response and gives the response in the output form to the user that has made the request.
5. What Are Filters?
Filters are methods that run “before”, “after” or “around” a controller action. Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application.
6. What Is Bundler?
Bundler is a new concept introduced in Rails3, which helps to you manage your gems for the application. After specifying gems in your Gemfile, you need to do a bundle install. If the gem is available in the system, bundle will use that else it will pick up.
7. How Many Types Of Relationships Does A Model Has?
has_one
belongs_to
has_many
has_many :through
8. Difference Between Render And Redirect?
render example: render :action, render :partial etc. redirect example: redirect_to :controller => ‘users’, :action => ‘new’
9. How To Serialize Data With Yaml?
YAML is a straight forward machine parsable data serialization format, designed for human readability and interaction with scripting language such as Perl and Python.
YAML is optimized for data serialization, formatted dumping, configuration files, log files, internet messaging and filtering.
10. How To Find Second Max Element From Database ?
Rails Query:Model.order("yourField DESC").limit(1).offset(1)
Mysql: SELECT * FROM yourTable ORDER BY yourField DESC LIMIT 1,1;
Other sql ways: -select max(column_name) from table_name where column_name<(select max(column_name) from table_name)
-SELECT Name FROM Employees group BY Salary DESCENDING limit 2;
11. What Are The Various Components Of Rail?
Action Pack: Action Pack is a single gem that contains Action Controller, Action View and Action Dispatch. The “VC” part of “MVC”.
Action Controller: Action Controller is the component that manages the controllers in a Rails application. The Action Controller framework processes incoming requests to a Rails application, extracts parameters, and dispatches them to the intended action.
Services provided by Action Controller include session management, template rendering, and redirect management.
Action View: Action View manages the views of your Rails application. It can create both HTML and XML output by default.
Action View manages rendering templates, including nested and partial templates, and includes built-in AJAX support.
Action Dispatch: Action Dispatch handles routing of web requests and dispatches them as you want, either to your application or any other Rack application. Rack applications are a more advanced topic and are covered in a separate guide called Rails on Rack.
Action Mailer: Action Mailer is a framework for building e-mail services. You can use Action Mailer to receive and process incoming email and send simple plain text or complex multipart emails based on flexible templates.
Active Model: Active Model provides a defined interface between the Action Pack gem services and Object Relationship Mapping gems such as Active Record. Active Model allows Rails to utilize other ORM frameworks in place of Active Record if your application needs this.
Active Record: Active Record are like Object Relational Mapping (ORM), where classes are mapped to table, objects are mapped to columns and object attributes are mapped to data in the table.
Active Resource: Active Resource provides a framework for managing the connection between business objects and RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.
Active Support: Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in Rails, both by the core code and by your applications.
12. What Is The Purpose Of Load, Auto_load, And Require_relative In Ruby ?
Load allows the process or a method to be loaded in the memory and it actually processes the execution of the program used in a separate file.
It includes the classes, modules, methods and other files that executes in the current scope that is being defined. It performs the inclusion operation and reprocesses the whole code every time the load is being called.
require is same as load but it loads code only once on first time.
Auto_load: this initiates the method that is in hat file and allows the interpreter to call the method.
require_relative: allows the loading to take place of the local folders and files.
13. What Are The Components Defined In The Model From Mvc Architecture?
The components involved in defining the model are as follows:
Validations: this is one of the very essential components and it defines the validations that are being put up on the input type of stream like validate_presence_of, format_of, etc.
Relationship: this is another type of component that describe the relationship between different types of components and it shows the relationship in the form of has_one, has_many, etc.
Callbacks: this is essential when it comes to respond after the failure and it allows the application to have certain functionality during failure. This can be given as before_save, after_save, etc.
Validation group settings: allow users to define the installed plugin settings.
Active record association relationship: allows current records to be actively having the relationship between one another.
14. What Are The Looping Structures Available In Ruby ?
for..in untill..end while..end do..end
Note: You can also use each to iterate a array as loop not exactly like loop
15. Resource Routing & Difference Between Member Routes And Collection Routes ?
Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, a resourceful route declares them in a single line of code.
Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as GET, POST, PUT and DELETE. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.

