You can check all 72 Ruby on Rails interview questions here ๐ https://devinterview.io/dev/rubyOnRails-interview-questions
ORM tends for Object-Relationship-Model, where Classes are mapped to table in the database, and Objects are directly mapped to the rows in the table.
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.
- Ruby: It is an object-oriented programming language inspired by PERL and Python
- Rails: It is a framework used for building web applications
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.
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.
- 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.
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
Ruby on Rails has been designed for creating a CRUD web application using MVC. Some of the features that Rails does not support include:
- Foreign key in databases
- Linking to multiple database at once
- Soap web services
- Connection to multiple database servers at once
The Rails controller is the logical center of the application. It facilitates 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
render causes rails to generate a response whose content is provided by rendering one of your templates. Means, it will direct goes to view page.
redirect_to generates a response that, instead of delivering content to the browser, just tells it to request another url. Means it first checks actions in controller and then goes to view page.
Helpers are modules that provide methods which are automatically usable in your view. They provide shortcuts to commonly used display code and a way for you to keep the programming out of your views. The purpose of a helper is to simplify the view.
- 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.
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.
While developing the projects, the users often have to write codes in the early stage of development. These codes help building the application in a very reliable manner and quickly and also, a close eye can be kept on the working of some major components with this approach. In Ruby, the scaffolding is done automatically and the users are free to concentrate on the core development only from the first day of development.
MVC tends for Model-View-Controller, used by many languages like PHP, Perl, Python etc. The flow goes like this:
Request first comes to the controller, controller finds and appropriate view and interacts with model, model interacts with your database and send the response to controller then controller based on the response give the output parameter to view.
- Ruby Instance variable begins with โ @
- Ruby Class variables begin with โ @@
- Ruby Global variables begin with โ $
Rails provides many features like:
- Meta-programming: Rails uses code generation but for heavy lifting it relies on meta-programming. Ruby is considered as one of the best language for Meta-programming.
- Active Record: It saves object to the database through Active Record Framework. The Rails version of Active Record identifies the column in a schema and automatically binds them to your domain objects using metaprogramming
- Scaffolding: Rails have an ability to create scaffolding or temporary code automatically
- Convention over configuration: Unlike other development framework, Rails does not require much configuration, if you follow the naming convention carefully
- Three environments: Rails comes with three default environment testing, development, and production.
- Built-in-testing: It supports code called harness and fixtures that make test cases to write and execute.
- Gem: A gem is a just ruby code. It is installed on a machine, and itโs available for all ruby applications running on that machine.
- Plugin: Plugin is also ruby code, but it is installed in the application folder and only available for that specific application.
When you have more than one model in your rails application, you would need to create connection between those models. You can do this via associations. Active Record supports three types of associations:
one-to-one: A one-to-one relationship exists when one item has exactly one of another item. For example, a person has exactly one birthday or a dog has exactly one owner.
one-to-many: A one-to-many relationship exists when a single object can be a member of many other objects. For instance, one subject can have many books.
many-to-many: A many-to-many relationship exists when the first object is related to one or more of a second object, and the second object is related to one or many of the first object.
You indicate these associations by adding declarations to your models:
has_one
,has_many
,belongs_to
,has_and_belongs_to_many
Ruby class libraries consist of a variety of domains, such as thread programming, data types, various domains, etc. These classes give flexible capabilities at a high level of abstraction, giving you the ability to create powerful Ruby scripts useful in a variety of problem domains. The following domains which have relevant class libraries are,
- GUI programming
- Network programming
- CGI Programming
- Text processing
Rails Migration can do following things
- Create table
- Drop table
- Rename table
- Add column
- Rename column
- Change column
- Remove column and so on
Consider:
resources :posts do
member do
get โmessagesโ
end
collection do
post โbulk_upload'
end
end
Defining routes with the resources
method automatically generates routes for the seven standard RESTful actions:
- GET /messages
- POST /messages
- GET /messages/new
- GET /messages/:id/edit
- GET /messages/:id
- PATCH/PUT /messages/:id
- DELETE /messages/:id
The difference in scope for these two variables is that:
@@name
is a class variable@name
is an instance variable
There are lot of things you can define in models, the few are:
- Validations (like validates_presence_of, numeracility_of, format_of etc.)
- Relationships (like has_one, has_many, HABTM etc.)
- Callbacks (like before_save, after_save, before_create etc.)
- Suppose you installed a plugin say validation_group, So you can also define validation_group settings in your model
- ROR Queries in Sql
- Active record Associations Relationship
The layouts removes code duplication in view layer. You are able to slice all your application pages to blocks such as header, footer, sidebar, body and etc.
A generated Rails application has a default layout and itโs defined in the app/views/layouts/application.html.erb
. On the screen above there is only one dynamic block - itโs the body, the footer, the header and the sidebar are common blocks for each page. So the code for the layout may look like this:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<%= render 'shared/header' %>
<%= render 'shared/sidebar' %>
<%= yield %>
<%= render 'shared/footer' %>
</html>
The yield
in the code above is the place in which any action template will be rendered. This is a default Rails behavior.
The purpose of this simple question is to make sure a developer is familiar with a test-driven development. A beginner may not have dealt with this file yet. The rakefile is similar to the makefile in Unix, and assists with packaging and testing Rails code. Itโs used by the rake utility, which ships natively with the Ruby installation.
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
Thanks ๐ for reading and good luck on your next tech interview!
Explore 3800+ dev interview question here ๐ Devinterview.io