Skip to content

alias_attribute: handle user defined source methods#52842

Merged
byroot merged 1 commit intorails:mainfrom
Shopify:active-model-alias
Sep 9, 2024
Merged

alias_attribute: handle user defined source methods#52842
byroot merged 1 commit intorails:mainfrom
Shopify:active-model-alias

Conversation

@casperisfine
Copy link
Contributor

Redo of: #52822
Fix: #52820

alias_attribute: handle user defined source methods

alias_attribute used to define a "jump method", e.g.
alias_attribute :foo, :bar was pretty much a macro to generate

def foo
  bar
end

This is convienient because it's easy, it doesn't impose an order
of declaration or anything like that.

But it's also much less efficient than a true alias_method.

It also used to cause cache size explosion which we fixed in
#52118, but making it behave
like Ruby's alias_method, by doing a real alias.

But this breaks some expectations (literally from the documentation):

  attr_accessor :name
  attribute_method_suffix '_short?'
  define_attribute_methods :name

  alias_attribute :nickname, :name

Here we're not supposed to alias a generated method, but a user defined one.

So this assumption can only hold for Active Record, not Active Model.

`alias_attribute` used to define a "jump method", e.g.
`alias_attribute :foo, :bar` was pretty much a macro to generate

```ruby
def foo
  bar
end
```

This is convienient because it's easy, it doesn't impose an order
of declaration or anything like that.

But it's also much less efficient than a true `alias_method`.

It also used to cause cache size explosion which we fixed in
rails#52118, but making it behave
like Ruby's `alias_method`, by doing a real alias.

But this breaks some expectations (literally from the documentation):

```ruby
  attr_accessor :name
  attribute_method_suffix '_short?'
  define_attribute_methods :name

  alias_attribute :nickname, :name
```

Here we're not supposed to alias a generated method, but a user defined one.

So this assumption can only hold for Active Record, not Active Model.
@krcm0209
Copy link

Are there any plans to backport this fix to 7.1.4.X?

@skipkayhil
Copy link
Member

Are there any plans to backport this fix to 7.1.4.X?

Yes, it has already been backported to 7-1-stable and will be available in the next 7.1 bugfix release (7.1.5)

@benlangfeld
Copy link

This was released in 7.1.5 and 7.2.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

alias_attribute broken in Rails 7.1.4

5 participants