Ruby on Rails 7: What's New?

Ruby on Rails 7: What’s New?

Ruby on Rails 7.0 was released on 15 December 2021. The new version comes with newly updated features that make web app backend development easy.

RoR has had a successful app development history since December 2005. So many popular apps such as Twitter, Shopify, Airbnb, etc. have trusted Ruby on Rails as a backend framework. 

And, due to easy programming language and simplified coding; RoR is always at the top of the developer’s choice. 

In this blog, we will see everything about what is Ruby on Rails, what is its version history, and the latest features updates in Ruby on Rails 7.

Let’s Begin!!

What Is Ruby on Rails?

A well-known Ruby on Rails is technically a Rails backend framework for web app development written in Ruby programming language. David Heinemeier Hansson has developed the RoR under MIT License.

RoR is compatible with MVC(model-view-controller) architecture that offers web pages, web services, and default database structure. 

Web standards like JSON or XML are used by RoR to transfer data and it also uses HTML, CSS, and JavaScript for developing user interface.

Ruby on Rails works on the two common principles, such as:

  • Don’t Repeat Yourself (DRY)
    To overcome the repetition of information and codes, Ruby on Rails runs on a DRY principle of software development.
  • Convention Over Configuration (CoC)
    It offers a number of opinions for the best way to perform many things in a developing web application.

The seventh version of the app web development framework Ruby on Rails, which has been around for 17 years, is currently available as well. 

In contrast to other web development frameworks, Ruby focuses on making coding straightforward and non-repetitive.

To start a project with RoR, you must contact ruby on rails development company, they will take your project and fulfill your web app requirements with the RoR framework.

Scan The Below QR, To Know More About Ruby on Rails!

Page Title
Hire RoR Developers on Hourly or Monthly Basis

The hourly rates start at $20/hour

Now, See the overall version history of Ruby on Rails;

Ruby on Rails Version History

Ruby on Rails was first made available in July 2004, the commit rights were not shared until February 2005. It would deploy Ruby on Rails alongside Mac OS X 10.5 “Leopard” in August 2006. 

Rails 7.0, the most recent version of Ruby on Rail, was released on December 15, 2021. This version includes the addition of the Asynchronous Querying, Encrypted Database Layer, Comparison Validator, and many more features to develop new web apps for the modern world.

Ruby on Rails Version History

New Updates of Ruby on Rails 7

The web development process will be made simpler by the exceptional features and improvements in the most recent version of Ruby on Rails.

The words of Rails creator David Hansson on the launch of Ruby on Rails 7 is “All the cards are on the table. No more fooling our sleeves. The culmination of many years of progress in five different aspects at once.”

Let’s start here of looking new features of rails 7:

1. Encrypted Database Layer

By utilizing the encrypts method on the ActiveRecord::Base; developers can encrypt and decrypt database fields with Rails 7. After your initial setup, Write the following code:

class Message < ApplicationRecord
encrypts :text
end

Encrypted Attributes Declaration

class Article < ApplicationRecord
encrypts :title
end

Before saving them to the database, the library will transparently encrypt the following characteristics; they will be decrypted upon retrieval:

article = Article.create title: “Encrypt it all!”
article.title # => “Encrypt it all!”

However, the performed SQL appears to be as follows:

INSERT INTO `articles` (`title`) VALUES
(‘{\”p\”:\”n7J0/ol+a7DRMeaE\”,\”h\”:{\”iv\”:\”DXZMDWUKfp3bg/Yu\”,\”at\”:\
“X1/YjMHbHD4talgF9dt61A==\”}}’)

Encryption Key Management

Important suppliers implement crucial management techniques. Key providers can be set up globally or for each individual characteristic.

1. Integrated Key Providers

a. DerivedSecretKeyProvider

a source of keys that PBKDF2 will produce using the given passwords.

config.active_record.encryption.key_provider =
ActiveRecord::Encryption::DerivedSecretKeyProvider.new([“some passwords”,
“to derive keys from. “, “These should be in”, “credentials”])

b. EnvelopeEncryptionKeyProvider

Every data encryption procedure generates a unique random key.

config.active_record.encryption.key_provider =
ActiveRecord::Encryption::EnvelopeEncryptionKeyProvider.new

2. Custom Key Providers

For more complex key-management systems, you can declare a custom key provider in an initializer:

ActiveRecord::Encryption.key_provider = MyKeyProvider.new

This interface must be implemented by a key provider:

class MyKeyProvider
def encryption_key
end
def decryption_keys(encrypted_message)
end
end

2. Comparison Validator

The comparison Validator determines and validates the object’s state before it goes to store in the database. 

It has been adequately checked taking into account all of its presence, uniqueness, numerical qualities, and validity of the specific data.

class Post < ApplicationRecord
validates :end_date, date: { after: Proc.new { Date.today } }
validates :end_date, date: { after: :start_date }
end

We should use custom_validator or gem_validator to validate the end_date.

After Rails 7 launched:

class Post < ApplicationRecord
validates_comparison_of :end_date, greater_than: -> { Date.today }
validates :end_date, greater_than: :start_date
end

3. Neither Webpack nor Node are necessary

The use of Webpack and Nodejs is not required by developers to utilize npm packages. There would be many processes involved in transiling ES6 and Babel, followed by bundling. 

If the developers want then Webpacker gem can be used by them to complete this work, however it came with extra baggage and was challenging to comprehend and modify.

The importmaps-rails gem now allows developers to import maps. Additionally, you can use./bin/importmap to update, pin, or unpin dependencies rather than creating code for package.json and installing dependencies using yarn or npm.

As an illustration, to install date-fns:

$ ./bin/importmap pin date-fns

By default this will add a line in config/importmap.rb like this:

in “date-fns”, to: “https://ga.jspm.io/npm:date-fns@2.27.0/esm/index.js”

You can continue writing code in your javascript like you always do:

import { formatDistance, subDays } from ‘date-fns’

formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true })
//=> “3 days ago”

When utilizing this structure, remember that there is no single interaction between the Ruby code you write and how the browser reads it.

It is completely okay to use because the majority of main browsers now support ES6. Only Typescript and JSK need to be translated into JS before implementation.

As a result, developers must employ the conventional approach of using webpack, esbuild, and rollup if they want to use React with JSX, for example.

This can be expedited for you by Rail 7. Just use the following command along with one of the selected tactics:

$ ./bin/rails javascript:install:[esbuild|rollup|webpack]

4. Asynchronous Querying

Developers can now retrieve results from data queries by using the load_async function. When numerous inquiries are asked at once, this is time-efficient. You can use this and run:

def PostsController
def index
@posts = Post.load_async
@categories = Category.load_async
end
end

This will execute the two queries together. That means, if each query’s execution time is of 100ms then, both queries will complete the execution in 100ms. It won’t take 200ms of time period for both individual queries.

5. Invert Where method

All of the defined scope criteria are reversed using the ApplicationRecord’s invert_where method.

Before Rails 7:

class User < ApplicationRecord
scope :verified, -> { where(email_verified: true, phone_verified:
true) }
scope :unverified, -> { where.not(email_verified: true,
phone_verified:true) }

scope :with_verified_email, -> { where(email_verified: true) }
scope :with_unverified_email, -> { where.not(email_verified: true) }
end

After Rails 7:

class User < ApplicationRecord
scope :verified, -> { where(email_verified: true, phone_verified: true)
}
scope :with_verified_email, -> { where(email_verified: true) }
end

We can just chain invert_where to verified and with_verified_email scopes rather than establishing unverified and with_unverified_email scopes with invalidate conditions.

Page Title
Hire RoR Developers on Hourly or Monthly Basis

The hourly rates start at $20/hour

6. Retry jobs indefinitely

The ActiveJob framework in Rails 7 adds the counter-failure feature, addressing the many reasons why queued jobs fail. This functionality allows for unlimited job retries. These causes include logical issues with the code, database issues, network issues, and queuing issues.

class AlertNotification < ActiveJon::Base
retry_on AlwaysRetryException, attempts: :unlimited
def perform()
# send an alert to user
end
end

To ensure the task always retries in case of failures, specify an unlimited value for the attempted argument when retrying the function.

7. Zeitwerk was a single operating mode for applications

Now, all Rail 7 app programmes will function in Zeitwerk mode. The management has made sure that this transition for the developers would go well. 

Ruby on Rails has a code loader called Zeitwerk. This allows the developers to quickly load the modules and classes for your project.

This has been started so that every project, programme, or other thing that depends on a gem can have a loader. The inflector, settings, and logger are unique to each loader. 

All of the capabilities of Classic mode are present in Zeitwerk; the main distinction is that Zeitwerk offers a more effective method for loading items.

8. Stimulus and Turbo have been replaced with UJS and Turbolinks

Applications utilising Rails 7 will now come pre-installed with Stimulus and Turbo (from Hotwire). Hotwire is a cutting-edge method for Ruby on Rails backend development of web apps that sends HTML rather than JSON over the wire and requires little to no JavaScript.

This results in quick page loads, the server running multiple templates at once, and productive work for developers. 

Turbo, a complementary Hotwire approach, utilizes WebSockets to transmit updates, simplifies complex pages into components, and accelerates page changes.

Hybrid technologies Turbo and Hotwire can both be included into iOS and Android. And Stimulus works in tandem with Turbo to offer a solution needed to quickly and effectively create appealing apps.

9. Use sole to inline your query with a single record

Developers can now assert that a query matches a single record by using first or find_by rather than sole or find_sole_by.

Product.where([“price = %?”, price]).sole
# => ActiveRecord::RecordNotFound (if no Product with given price)
# => #<Product …> (if one Product with given price)
# => ActiveRecord::SoleRecordExceeded (if more than one Product with given price)
user.api_keys.find_sole_by(key: key)
# as above

10. Utilize controller operations to the files generated from the stream

Using send_stream inside a controller action in Ruby on Rails 7, you can speed up a file created on the fly.

send_stream(filename: “subscribers.csv”) do |stream|
stream.write “email_address,updated_at\n”
@subscribers.find_each do |subscriber|
stream.write “#{subscriber.email_address},#{subscriber.updated_at}\n”
end
end

When implemented on Heroku, this gives developers an immediate or partial answer to let them know that something is going on.

If you want to ensure that all the functions get done accurately in your ongoing developing application from a developer; then you should hire ruby on rails developers with a great experience.

11. Named Variants

In the most recent version of Ruby on Rails 7, variations can be named using ActiveStorage.

class User < ApplicationRecord
has_one_attached :avatar do |attachable|
attachable.variant :thumb, resize: “100×100”
end
end

#Call avatar.variant(:thumb) to get a thumb variant of an avatar:
<%= image_tag user.avatar.variant(:thumb) %>

Final Words

Ruby on Rails 7, with its exciting features, launches into the world of modern applications, providing all the necessary tools for today’s developers. And also offered simplified coding that is beneficial for developers. 

Check out the Rails 7 release note to get a complete list of the new features, bug fixes, and modifications. Although not very thorough, these will eventually be updated. The newest improvements and features are now available for Rail 7.

If you want to look forward to Ruby on Rails web app development for your business product; then it’s time to contact Ruby on Rails Consulting Services that will help you to find the best RoR company or developers who will take your RoR project and create the best app for you.

So many apps have already trusted Ruby on Rails. Now it’s your turn to take a chance and get positive outcomes.

HAVE A GOOD EXPERIENCE WITH RAILS 7!!!!

Page Title
Hire RoR Developers on Hourly or Monthly Basis

The hourly rates start at $20/hour

Frequently Asked Questions (FAQs)

Mitul Patel
Mitul Patel
www.rorbits.com/

Mitul Patel, Founded RORBits, one of the Top Software Development Company in 2011 offer mobile app development services across the globe. His visionary leadership and flamboyant management style have yield fruitful results for the company.

Leave a Reply

Your email address will not be published.Required fields are marked *

×