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.
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.
Ruby: It is an object-oriented programming language inspired by PERL and Python.
Rails: This is a framework used to build web applications.
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.
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).
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.
In the Gemfile, we specify the dependencies for the Ruby application. It is located in the root directory of the project.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Ruby does not allow you to inherit from more than one parent class, but it does allow include and extend module mixins.
Ruby is strongly typed. If you try to evaluate "hello" + 3, an error will be thrown.
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.
Helper logic should only support views.
A good candidate for a helper is date formatting logic, which is required in several different representations.
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.
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.
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.
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.
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