You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When upgrading an app from Rails 7.0 to Rails 7.1 I'm getting these warnings:
DEPRECATION WARNING: MyModel model aliases `t`, but `t` is not an attribute.
Starting in Rails 7.2, alias_attribute with non-attribute targets will raise.
The attribute I want to alias is defined in an Active Record Store.
This behavior was introduced in this PR 48972.
Steps to reproduce
# frozen_string_literal: truerequire"bundler/inline"gemfile(true)dosource"https://rubygems.org"git_source(:github){ |repo| "https://github.com/#{repo}.git"}# Activate the gem you are reporting the issue against.gem"activerecord","~> 7.1.2"gem"sqlite3"endrequire"active_record"require"minitest/autorun"require"logger"# This connection will do for database-independent bug reports.ActiveRecord::Base.establish_connection(adapter: "sqlite3",database: ":memory:")ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedocreate_table:my_models,force: truedo |t|
t.string:propertiesendendclassMyModel < ActiveRecord::Basestore:properties,accessors: [:t],coder: JSONalias_attribute:title,:tendclassBugTest < Minitest::Testdeftest_alias_attributemy_model=MyModel.create!(title: "My title")assert_equalmy_model.title,"My title"assert_equalmy_model.t,"My title"endend
Expected behavior
No warnings are given.
Actual behavior
-- create_table(:my_models, {:force=>true})
Run options: --seed 9361
# Running:
DEPRECATION WARNING: MyModel model aliases `t`, but `t` is not an attribute. Starting in Rails 7.2, alias_attribute with non-attribute targets will raise. Use `alias_method :title, :t` or define the method manually.
Finished in 0.004296s, 232.7610 runs/s, 465.5219 assertions/s.
1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
This is a bit tricky. The deprecation should be correct and aliasing AR store attributes should be broken once we drop the deprecation because store attributes implement direct reader&writer and don't go through the attribute(:attr_name) method to get the value
So just silencing the deprecation won't work, in order to fix this we need to either change AR store or provide a new helper to define an AR store attribute alias (suggestion n2) but unfortunately none of these changes can be back-ported so we may need to consider delaying the removal of this deprecation if aliasing AR store attributes is essential and must continue to work.
I'm not very familiar with AR internals. But I figured that alias_attribute only defines some methods to access an existing store attribute so nothing bad would happen. If the changes in Rails 7.2 would break this particular situation then just suppressing the deprecation warning is obviously not going to work.
A bit of background why I'm using these aliases. I have some attributes in an AR store where the proper name is quite long (like: 'general_product_category') and the table has a lot of records (> 100M). Just using a single character as the attribute name saves a lot of storage space for such a large table. This is certainly true when using a text or json database field and probably true when using a (postgres) jsonb field.
For me having aliases on store attributes is essential. But on the other hand easy to work around by adding a getter and a setter for each attribute (using define_method). Basically suggestion # 2 in a model concern.
When upgrading an app from Rails 7.0 to Rails 7.1 I'm getting these warnings:
The attribute I want to alias is defined in an Active Record Store.
This behavior was introduced in this PR 48972.
Steps to reproduce
Expected behavior
No warnings are given.
Actual behavior
System configuration
Rails version: 7.1.2
Ruby version: 3.2.2
Possible solutions
alias_store_attribute
which adds getter and setter methods for the aliased nameThe text was updated successfully, but these errors were encountered: