Github ActiveRecord::AttributeMethods::Query respect getter overwrites in the mo...
source link: https://github.com/rails/rails/pull/40782
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.
Changelog
-
Makes ActiveRecord::AttributeMethods::Query respect the getter overrides defined in the model.
Fixes #40771
Before:
class User def admin false # Overriding the getter to always return false end end user = User.first user.update(admin: true) user.admin # false (as expected, due to the getter overwrite) user.admin? # true (not expected, returned the DB column value)
After this commit,
user.admin?
above returns false, as expected.
Summary
As discussed in #40771 , ActiveRecord::AttributeMethods::Query
didn't respect the getter overrides defined in the model.
Assuming the User model has boolean column named admin:
class User def admin false # Overriding the getter to always return false end end user = User.first user.update(admin: true) user[:admin] # true (as expected) user.read_attribute(:admin) # true (as expected) user.admin # false (as expected) user.admin? # true (not expected)
This commit is a one-liner that makes
usevalue = public_send(attr_name)
instead of value = self[attr_name]
, so instead of going directly to the column value, it goes through the model to respect any getter overwrites.I also updated tests and documentation.
Pinging @rafaelfranca as we had initiated a discussion on the issue above.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK