Rails 7.1 - Raises on assignment to readonly attributes
source link: https://blog.kiprosh.com/rails-7-1-raises-on-assignment-to-readonly-attributes/
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.
rails_7 Published on 12 January 2023 12 January 2023 • 1 min read
Rails 7.1 - Raises on assignment to readonly attributes
Rails 7.1 adds the raise_on_assign_to_attr_readonly
config to config.active_record
that is used to raise ActiveRecord::ReadonlyAttributeError
error on assignment to attr_readonly
attributes. The previous behavior would allow assignment but silently not persist changes to the database.
Configuring config.active_record.raise_on_assign_to_attr_readonly
In a newly created Rails 7.1 application, config.load_defaults 7.1
is set by default in application.rb
. The default value of raise_on_assign_to_attr_readonly
for config.load_defaults 7.1
is true
and for config.load_defaults < 7.1
it is false
. To opt-in:
# config/environments/application.rb
config.active_record.raise_on_assign_to_attr_readonly = true
Let's take an example of the following Post model structure:
# app/models/post.rb
class Post < ActiveRecord::Base
attr_readonly :title
end
-
When
config.active_record.raise_on_assign_to_attr_readonly = true
post = Post.create!(title: "Introducing Ruby on Rails!") post.title = "a different title" # raises ActiveRecord::ReadonlyAttributeError post.update(title: "a different title") # raises ActiveRecord::ReadonlyAttributeError
-
When
config.active_record.raise_on_assign_to_attr_readonly = false
post = Post.create!(title: "Introducing Ruby on Rails!") post.title = "a different title" # does not raise any error post.update(title: "a different title") # change to title will be ignored
References
- Rails PR#46105 & Rails PR#46602 - Raise on assignment to readonly attributes
Sampat Badhe
I am a Rubyist and Node.js developer at Kiprosh.com. I enjoy building innovative applications and currently in love with TDD practice. I am practicing Agile and love to pair program.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK