Skip to content

Instantly share code, notes, and snippets.

@such
Last active July 7, 2022 13:51
Show Gist options
  • Save such/175b943f10587902173cbcada655a96d to your computer and use it in GitHub Desktop.
Save such/175b943f10587902173cbcada655a96d to your computer and use it in GitHub Desktop.
Rails bug reproduction:
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "7.0.2.4"
gem "sqlite3"
end
require "active_record"
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.define do
create_table :cards, force: true do |t|
t.references :card_data
end
create_table :card_data, force: true do |t|
t.references :player
end
create_table :players, force: true do |t|
t.references :active_club
end
create_table :teams, force: true do |t|
t.string :type
end
end
class Card < ActiveRecord::Base
belongs_to :card_data
has_one :player, through: :card_data
has_one :active_club, through: :player
end
class CardData < ActiveRecord::Base
belongs_to :player
end
class Player < ActiveRecord::Base
belongs_to :active_club, class_name: 'Club'
end
class Team < ActiveRecord::Base
end
class Club < Team
end
player = Player.create!
card_data = CardData.create!(player:)
card = Card.create!(card_data:)
card.reload.player
::ActiveRecord::Associations::Preloader.new(records: [card], associations: :active_club).call.then(&:first)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment