Skip to content

Commit 743a75e

Browse files
committed
docs: document alias_attribute behavior change in Rails 7.2 upgrading guide
1 parent f289e25 commit 743a75e

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

guides/source/upgrading_ruby_on_rails.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,60 @@ set the `queue_adapter` config to something other than `:test`, but written test
104104

105105
If no config is provided, the `TestAdapter` will continue to be used.
106106

107+
### `alias_attribute` now bypasses custom methods on the original attribute
108+
109+
In Rails 7.2, `alias_attribute` now bypasses custom methods defined on the original attribute and directly accesses the underlying database value. This change was announced via deprecation warnings in Rails 7.1.
110+
111+
**Before (Rails 7.1):**
112+
113+
```ruby
114+
class User < ActiveRecord::Base
115+
def email
116+
"custom_#{super}"
117+
end
118+
119+
alias_attribute :username, :email
120+
end
121+
122+
user = User.create!(email: "[email protected]")
123+
user.username
124+
125+
```
126+
127+
**After (Rails 7.2):**
128+
129+
```ruby
130+
user = User.create!(email: "[email protected]")
131+
user.username
132+
# => "[email protected]" # Raw database value
133+
```
134+
135+
If you received the deprecation warning "Since Rails 7.2 `#{method_name}` will not be calling `#{target_name}` anymore", you should manually define the alias method:
136+
137+
```ruby
138+
class User < ActiveRecord::Base
139+
def email
140+
"custom_#{super}"
141+
end
142+
143+
def username
144+
email # This will call the custom email method
145+
end
146+
end
147+
```
148+
149+
Alternatively, you can use `alias_method`:
150+
151+
```ruby
152+
class User < ActiveRecord::Base
153+
def email
154+
"custom_#{super}"
155+
end
156+
157+
alias_method :username, :email
158+
end
159+
```
160+
107161
Upgrading from Rails 7.0 to Rails 7.1
108162
-------------------------------------
109163

0 commit comments

Comments
 (0)