Rails provides better ActiveRecord enum assertions
source link: https://blog.saeloun.com/2022/03/02/rails-add-better-active-record-enum-assertions
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ActiveRecord’s enum provides an easy way to map values to integers. However, it has been lagging in the built-in assertions available. Recently, Rails added options to prevent enum values from being defined erroneously. This does not covert the plethora of possible errors that can be encountered with enums.
Before
It was possible to set enum to empty values. This does not cause a drastic failure however it opens up a vulnerability. Further, it was also possible to define erroneous values to an enum like numbers or empty hashes.
Let’s see how the current implementation looks like. Consider this ActiveRecord model.
class Blog < ApplicationRecord
enum :status
validates :status, inclusion: {in: ["draft", "reviewed", "published"]}
end
Let’s instantiate a new object.
> Blog.new
=> #<Blog id: nil, title: nil, status: nil, created_at: nil, updated_at: nil>
> Blog.new(status: "review")
=> Traceback (most recent call last):
1: from (irb):2
ArgumentError ('review' is not a valid status)
As you can see an ArgumentError error is only thrown for erroneous values.
After
Thanks to this PR the missing assertions have been added.
Now let’s see what happens when instantiating a new object.
> Blog.new
=> Traceback (most recent call last):
4: from (irb):1
3: from (irb):2:in `rescue in irb_binding'
2: from app/models/blog.rb:14:in `<main>'
1: from app/models/blog.rb:17:in `<class:Blog>'
ArgumentError (Enum values [] must not be empty.)
It preemptively catches errors now! Further, a clearer message is now given when setting enum values to Integer options.
class Blog < ApplicationRecord
enum status: ['abc', 1]
end
This is what happens,
> Blog.new
=> Traceback (most recent call last):
3: from (irb):1
2: from app/models/blog.rb:14:in `<main>'
1: from app/models/blog.rb:17:in `<class:Blog>'
ArgumentError (Enum values ["abc", 1] must only contain symbols or strings.)
Prior to this change a generic error statement used to be raised which read
ArgumentError (Enum values ["abc", 1] must be either a hash, an array of symbols, or an array of strings.)
.
The updated error statement highlights the actual error in value setting.
Recommend
-
13
Postgres Indexes for ActiveRecord Join Tables in Rails Apps Updated Jun 23, 2020 3 comments 9 minute read ...
-
9
UUID Primary Key in Rails 6 with PostgreSQL and Active Record Updated May 27, 2020 16 comments 8 minute read ...
-
4
Rails ActiveRecord PostgreSQL Foreign Keys and Data Integrity Updated Jun 28, 2019 5 minute read
-
10
Implementing Safe ActiveRecord Like Queries for Rails Nov 11, 2019 In any SQL based database, a like query is generally an SQL injection attack waiting to happen because the underlying sql statement looks like...
-
7
A Tricky Bit of Rails ActiveRecord Optimization Nov 17, 2016 Yesterday I discovered that queries on a table of N rows were sometimes taking in excess of a few minutes to execute. N was fairly large – my guestimate woul...
-
10
Published on 11 March 2021 in ruby-on-rails ActiveRecord attribute encryption in Ruby on Rails for better security compliance Data Encryptio...
-
21
Latest changes in Rails 7 introduces new syntax for defining an enum. Before The previous syntax includes the key value pair for the enum name, its enum values, and the...
-
5
filesCore extensionsAbstractControllerActionCableActionControllerActionDispa...
-
5
Rails 7 extends enum support for Postgres adapter Jun 21, 2023 •
-
4
How to use enum attributes in Ruby on Rails Dive into the world of enums to discover how they can streamline your code and make it more readable. Learn best practices, how...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK