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
# frozen_string_literal: truerequire"bundler/inline"gemfile(true)dosource"https://rubygems.org"git_source(:github){ |repo| "https://github.com/#{repo}.git"}gem"rails"gem"sqlite3"endrequire"active_record"require"minitest/autorun"require"logger"ActiveRecord::Base.establish_connection(adapter: "sqlite3",database: ":memory:")ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedocreate_table:posts,force: truedo |t|
t.string:titlet.string:exclusive_chile_columnt.string:typeendendclassPost < ActiveRecord::BaseendclassPost::Chile < PostendclassPost::Peru < Postself.ignored_columns += [:exclusive_chile_column]endclassBugTest < Minitest::Testdeftest_association_stuffpost_peru=Post::Peru.create!(title: 'hola')# Correct behavior, the column is ignored when assigning the attributes from sti classpost_peru_called_from_sti=Post::Peru.find(post_peru.id)assertpost_peru_called_from_sti.attributes.keys.exclude?('exclusive_chile_column')assertpost_peru_called_from_sti.attributes.keys.size == Post::Peru.column_names.size# This is the bug, the column is not being ignored when assigning the attributes from base classpost_peru_called_from_base_class=Post.find(post_peru.id)assertpost_peru_called_from_base_class.instance_of?Post::Peru# fails assertpost_peru_called_from_base_class.attributes.keys.exclude?('exclusive_chile_column')# also fails assertpost_peru_called_from_base_class.attributes.keys.size == Post::Peru.column_names.sizeendend
Expected behavior
Sti classes when instantiated from the base_class on quering should not have ignored columns on the attributes associated with the records
Actual behavior
Sti classes when instantiated from the base_class on quering method have ignored columns associated whose is inconsistent with the behaviour on calling the same quering method directly on the sti class
This inconsistency results in instances of the STI class having different attributes, as demonstrated in the script above.
System configuration
Rails version: 7.1.2
Ruby version: 3.2.2
Contributing
I am considering submitting a pull request to address this issue because we use a monkey patch on our software. However, before proceeding, I would like to hear your thoughts. Do you think this is something that can be changed?
The text was updated successfully, but these errors were encountered:
Previously, querying from the base class resulted in an object of the inherited type,
with 'ignored_columns' included in the 'attributes' method.
This behavior was inconsistent, as it did not occur when querying directly from the inherited class.
This fix ensures consistent behavior in class instantiation during queries.
Previously, querying from the base class resulted in an object of the inherited type,
with 'ignored_columns' included in the 'attributes' method.
This behavior was inconsistent, as it did not occur when querying directly from the inherited class.
This fix ensures consistent behavior in class instantiation during queries.
jvlara
added a commit
to jvlara/rails
that referenced
this issue
Jan 11, 2024
Previously, querying from the base class resulted in an object of the inherited type,
with 'ignored_columns' included in the 'attributes' method.
This behavior was inconsistent, as it did not occur when querying directly from the inherited class.
This fix ensures consistent behavior in class instantiation during queries.
Steps to reproduce
Expected behavior
Sti classes when instantiated from the base_class on quering should not have ignored columns on the attributes associated with the records
Actual behavior
Sti classes when instantiated from the base_class on quering method have ignored columns associated whose is inconsistent with the behaviour on calling the same quering method directly on the sti class
This inconsistency results in instances of the STI class having different attributes, as demonstrated in the script above.
System configuration
Rails version: 7.1.2
Ruby version: 3.2.2
Contributing
I am considering submitting a pull request to address this issue because we use a monkey patch on our software. However, before proceeding, I would like to hear your thoughts. Do you think this is something that can be changed?
The text was updated successfully, but these errors were encountered: