01
ORM tends towards an Object Relationship Model where classes map to a table in a database and objects map directly to rows in a table.
02
Rake is Rubin's brand; it is a Ruby utility that replaces the Unix "make" utility and uses the "Rakefile" and ".rake" files to create a task list. In Rails, Rake is used for common administrative tasks such as migrating a database using scripts, loading a schema into a database, and so on.
03
Ruby: It is an object-oriented programming language inspired by PERL and Python.
Rails: This is a framework used to build web applications.
04
Ruby is dynamically typed. This is why you can change the type of a variable on the fly.
In Ruby, the following lines of code, executed one after the other, will not cause errors.
05
Getter allows you to access an instance variable. A setter allows you to set an instance variable.
You can manually define the getter and setter methods.
But Ruby provides three accessors that do the same and cleaner ones: attr_reader (getter), attr_writer (setter), and attr_accessor (setter and getter).
06
A message containing the name of the method is sent to the object. If this method exists for an object, the object calls it.
This will become more obvious when you consider the send method in Ruby.
07
In the Gemfile, we specify the dependencies for the Ruby application. It is located in the root directory of the project.
08
It contains records of the exact versions of the gemstones installed. This is so that the same versions can be installed if another computer clones the project.
In contrast, specifying the gem in the Gemfile without being tied to a specific version will simply install the latest version of the gem.
09
MVC (Model-View-Controller) is the software design pattern that Rails is built on top of. He divides information processing into three parts.
The model manages data and logic. The view displays information. The controller takes input and prepares the data for the model or view.
10
Active Record is an ORM (Object Relational Mapping) that maps models to database tables. This makes it easier to customize the application as we no longer need to write SQL directly to load, save, or delete objects.
It also provides some protection against SQL injection.
01
Authorization (not to be confused with authentication) refers to allowing different types of users to have different levels of access in an application. This is useful when there are many types of users with different access levels.
Several gems like Pundit and CanCanCan implement authorization.
02
Callback is a misleading term. They are hooks in the lifecycle of an object on which you can execute methods.
There are a number of callbacks for creating, updating and destroying an object, such as before_validation, after_save, and after_destroy.
They are useful for conditional logic such as creating a linked contact record when creating a user record.
03
Updating an object after it has been saved requires an additional database transaction to save the update. So, if you are updating an attribute of an object, the before_save callback is more efficient.
But sometimes information about an object exists until it is stored (for example, id). So, if an id is required to create a linked post, that should be done in the after_save callback.
04
Initializers contain configuration logic and are only run when the application is loaded. This means that the Rails server needs to be restarted if the initializers are changed. They are located in the / config / initializers directory.
05
The most common destruction callback in Rails applications is specified in associations in model files. For example, the example below removes related comments when the article is destroyed.
06
Business logic must exist in models, not controllers. This makes it easier to unit test your logic and makes it more reusable.
Controllers are just hands passing information between views and models.
This is usually given as advice to new Rails developers. This is actually not recommended, especially in large applications.
07
As the codebase grows, fat models get out of hand, do too many things, and become unmanageable. Models need to be persistent without being overloaded with logic.
Models can be thinned by keeping the Single Responsibility Principle in mind and moving the logic out of the models to other design patterns such as utility objects.
08
Class methods are available in classes, and instance methods are available in instances (of course). They are usually used for different purposes.
For the Article class, the instance method can count the number of words in the body of a particular article. Although the class method can count the number of articles by a particular author in all articles (notice the difference in volume?).
Class methods are denoted by def self.method_name.
09
The developer manually creates and adds instructions to the migration files.
They tell ActiveRecord how to change the existing state of the database. For this reason, deleting or modifying previous migrations can degrade the database and is not recommended.
Creating migration files manually differs from other frameworks like Django, where you specify the final state of the database and then migrations are automatically generated to make the necessary changes.
10
There are a number of design patterns in Rails, including utility objects, value objects, form objects, request objects, view objects, policy objects, and decorators.
A detailed description of each is the topic of an entire post, but here's a great tutorial with examples.
01
Ruby does not allow you to inherit from more than one parent class, but it does allow include and extend module mixins.
02
Ruby is strongly typed. If you try to evaluate "hello" + 3, an error will be thrown.
Basically, JavaScript is loosely typed and will just evaluate the same calculation as "hello3".
03
04
A constructor is defined using an initialization method that is called when a new instance of the class is initialized. No definition of this method is required. It is often used to provide attribute values in new instances.
05
Helper logic should only support views.
A good candidate for a helper is date formatting logic, which is required in several different representations.
06
Use self when defining and calling class methods.
In a class, self refers to the current class, so it is required when a class method calls another class method.
self.class.method is required when an instance calls a class method.
07
Rack is an API that sits between the web server and Rails. It allows you to connect and exchange frameworks like Rails with Sinatra, or web servers like Unicorn with Puma.
08
Both procs and lambda expressions are stored blocks, but the syntax and behavior are slightly different.
The lambda returns from itself, but the procedure returns from the method it is in.
Note that method_proc returns 1 because the call to the procedure ends execution inside the method.
09
PORO stands for Plain Old Ruby Object.
While almost everything in Ruby is an object, ActiveRecord tends to use many complex objects. Thus, the term PORO usually means a small, simple object used to support business logic.
10
There are hundreds of battle-proven software development experts in our Talent Network.
Are you a Ruby on Rails developer looking for amazing projects? Join as a Talent