Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When quering on a base class that has inheritance, childs instantiate with full attributes including ignored columns #50499

Open
jvlara opened this issue Dec 31, 2023 · 2 comments

Comments

@jvlara
Copy link

jvlara commented Dec 31, 2023

Steps to reproduce

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
  gem "rails"
  gem "sqlite3"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.string :title
    t.string :exclusive_chile_column
    t.string :type
  end
end

class Post < ActiveRecord::Base
end

class Post::Chile < Post
end

class Post::Peru < Post
  self.ignored_columns += [:exclusive_chile_column]
end



class BugTest < Minitest::Test
  def test_association_stuff
    post_peru = Post::Peru.create!(title: 'hola')


    # Correct behavior, the column is ignored when assigning the attributes from sti class
    post_peru_called_from_sti = Post::Peru.find(post_peru.id)
    assert post_peru_called_from_sti.attributes.keys.exclude?('exclusive_chile_column')
    assert post_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 class
    post_peru_called_from_base_class = Post.find(post_peru.id)
    assert post_peru_called_from_base_class.instance_of? Post::Peru
    # fails 
    assert post_peru_called_from_base_class.attributes.keys.exclude?('exclusive_chile_column')
    # also fails 
    assert post_peru_called_from_base_class.attributes.keys.size == Post::Peru.column_names.size
  
  end
end

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?

@fatkodima
Copy link
Member

Looks like a bug to me. Please, do open a PR.

jvlara added a commit to jvlara/rails that referenced this issue Jan 3, 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.
@jvlara
Copy link
Author

jvlara commented Jan 3, 2024

Thanks, here is the PR. Happy to recieve some feedback 👌

jvlara added a commit to jvlara/rails that referenced this issue Jan 5, 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.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants