Rails 6-Noteworthy new points

Tram Ho

Hi,

Hi everybody. Today I will share with you a few interesting points about Rails 6 – the latest version of the Rails framework. The reason I wrote this article is because in the past month I was so faced with the project, I did not have time to learn more or more about coding to write a tutor, convenient before participating in a small project. using Rails 6. So I spend time reading more about Rails 6 after a few people just “should read”

Overall, the release of Rails 6 provides a number of additional features to assist in the development of website applications as well as api. In the article I would like to keep the original English with the items I will note after reading the documents. Because actually, I also have difficulty translating those phrases clearly.

Start point by point!

Active Record

The most notable point is the feature to connect with multiple database-databases in the same Rails application. This has been introduced earlier.

Eileen M. Uchitelle has inherited from Github’s codebase to include the multi database switch feature in Rails 6. It provides us a pretty convenient solution for converting connections to database repos and queries.

Part of the original source code, you can follow at this

It is quite simple and easy to read, I will give some examples for you to be more specific.

As above we can see that AnimalsModel model is configured to connect directly to the database with different writing and reading permissions.

In the database.yml file, we conduct a simple config as follows:

Gannon McGibbon then added newer code sets to support the hash and url config in ActiveRecord::Base.connected_to ‘s database hash to further refine this functionality. Below that the code demonstrates this feature:

The pick-function is similar to the pluck function, but for single record

This one I think is quite good. Instead of normal, you will use select or something to get out the attributes you need in a record. Rails 6 has added a nice function to support this which is pick.

Looking at the example, you understand, right?

The pick method itself is a select statement that retrieves the fields you want.

ActiveRecord :: Base.create_or_find_by /! add the query select or insert feature

Looking at the name of the function, I think you also understand the function: It executes create or find_by at the same time, the condition for find_by is to rescue of create. You can read through the source code below to learn more:

Make the implicit order column configurable

Usually when dealing with list records, we often use functions to get values ​​in different positions. Rails 6 supports us a feature to implicitly order those records on a certain basis: it can be attributes of that model.

As above, the created_at attribute is used to serve as the order basis for the User list.

Action Mailbox

Rails 6 provides a suite of applications that allow developers to integrate more convenient and easier email features called Action Mailbox . All these cool features will be fully provided in ActionMailbox::Base . You can extend it through another class called ApplicationMailbox .

You can see more of the document at this

Pass along arguments to underlying GET method in

#follow_redirect!

Normally with verses under 6 of Rails, we feel quite cumbersome when we want to redirect on the server and include params. But with Rails 6’s follow_redirect , you can easily pass the parameters for the GET method by passing those parameters into follow_redirect .

Action View

Add allocations to template rendering instrumentation

In Rails 6, developers can easily know the time of a task from start to finish. It reports a very precise number (seconds). This greatly aids the development of functions and optimizes the performance of the task as well as the application. We take a look at the Rails log below:

Active Job

Allows configurable attribute name for #has_secure_password

#has_secure_password is an identifier method for attributes that need to be encoded in the application, most commonly passwords. It adds a very simple encoding feature. All you need to do is to specify the required attribute in the respective model.

Action Cable

The last impression I want to share with you is Action cable in Rails 6. I must say this is the most impressive change for me, since before in the older versions, I felt quite difficult when touch the nun. For example, developing realtime chat function

But in Rails 6 everything is much easier. The action cable is built-in directly, which means you can use it just by creating the chanels for your purpose.

Above are a few impressive points I want to introduce to you. Of course, Rails 6 has quite a few more interesting things that you can study more. For example: Action Mailer, Action Support, Zeitwerk, Active Storage...

Thank you for referencing through my article! Bye bye

Share the news now

Source : Viblo