Skip to content

Conversation

@seanpdoyle
Copy link
Contributor

Replace all access of @attributes and attributes key-value pairs with calls to read_attribute and write_attribute.

The read_attribute and write_attribute implementations draw inspiration from ActiveRecord::AttributeMethods::Read and ActiveRecord::AttributeMethods::Write, respectively.

rails/rails#53886 proposes implementing each method at the Active Model layer. While that proposal is considered, this commit implements each method in terms of accessing the underlying @attributes hash instance. This change is also in support of a first-party integration with ActiveModel::Attributes proposed in #410, and aims to be compatible with its attribute reading and writing interfaces.

person = Person.find(1)
person.read_attribute("name")           # => "Matz"
person.name                             # => "Matz"

person.write_attribute("name", "matz")
person.name                             # => "matz"

@seanpdoyle seanpdoyle force-pushed the read-write-attributes branch from b897313 to 89a9a18 Compare November 18, 2025 19:52
@guilleiguaran
Copy link
Member

Please fix conflicts to merge this!

Replace all access of `@attributes` and `attributes` key-value pairs
with calls to `read_attribute` and `write_attribute`.

The `read_attribute` and `write_attribute` implementations draw
inspiration from [ActiveRecord::AttributeMethods::Read][] and
[ActiveRecord::AttributeMethods::Write][], respectively.

[rails/rails#53886][] proposes implementing each method at the Active
Model layer. While that proposal is considered, this commit implements
each method in terms of accessing the underlying `@attributes` hash
instance. This change is also in support of a first-party integration
with [ActiveModel::Attributes][] proposed in [rails#410][], and aims to be
compatible with its attribute reading and writing interfaces.

```ruby
person = Person.find(1)
person.read_attribute("name")           # => "Matz"
person.name                             # => "Matz"

person.write_attribute("name", "matz")
person.name                             # => "matz"
```

[ActiveRecord::AttributeMethods::Read]: https://edgeapi.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Read.html#method-i-read_attribute
[ActiveRecord::AttributeMethods::Write]: https://edgeapi.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Write.html#method-i-write_attribute
[rails/rails#53886]: rails/rails#53886
[ActiveModel::Attributes]: https://edgeapi.rubyonrails.org/classes/ActiveModel/Attributes.html
[rails#410]: rails#410
@seanpdoyle seanpdoyle force-pushed the read-write-attributes branch from 89a9a18 to 2b46124 Compare November 19, 2025 04:13
@seanpdoyle
Copy link
Contributor Author

Thank you for the review @guilleiguaran

@guilleiguaran guilleiguaran merged commit eb89c99 into rails:main Nov 19, 2025
19 checks passed
@seanpdoyle seanpdoyle deleted the read-write-attributes branch November 19, 2025 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants