Open tagliala opened 4 months ago
Looks like that the preloader is creating this issue:
SELECT
"members".*
FROM
(
SELECT
"history"."members".*
FROM
"history"."members"
WHERE
(
'2024-06-01 17:35:36.306466' :: timestamp < @ history.members.validity
)
) "members"
INNER JOIN "councils" ON "members"."council_id" = "councils"."id"
WHERE
"councils"."country_id" = $1
AND "members"."active" = $2
LIMIT
$3 [[ "country_id",
1],
[ "active",
true],
[ "LIMIT",
1]]
~Do we need a patch for active_record/associations/preloader/branch.rb
?~
The problem is somewhere in the Preloader
Models
Seeds
Output in the present
Output in history
Test case
```rb # frozen_string_literal: true require 'bundler/inline' gemfile(true) do source 'https://rubygems.org' gem 'chrono_model' # Test against latest Chronomodel: # gem 'chrono_model', github: 'ifad/chronomodel' gem 'pg' end require 'chrono_model' require 'minitest/autorun' require 'logger' # Needs a database called `chronomodel_test` ActiveRecord::Base.establish_connection(adapter: 'chronomodel', database: 'chronomodel_test') ActiveRecord::Base.logger = Logger.new($stdout) ActiveRecord::Schema.define do enable_extension :btree_gist create_table :countries, temporal: true, force: true do |t| t.string :name t.timestamps end create_table :councils, temporal: true, force: true do |t| t.references :country t.string :name t.timestamps end create_table :members, temporal: true, force: true do |t| t.references :council t.string :name t.boolean :active, default: true t.timestamps end end class Country < ActiveRecord::Base include ChronoModel::TimeMachine has_many :cities, dependent: :destroy has_one :council has_one :first_member, through: :council end class Council < ActiveRecord::Base include ChronoModel::TimeMachine belongs_to :country has_many :members, dependent: :destroy # Problem is caused by `where` in the `scope`. `order` does not cause issues has_one :first_member, -> { where(active: true) }, class_name: 'Member', dependent: nil, inverse_of: false end class Member < ActiveRecord::Base include ChronoModel::TimeMachine belongs_to :council has_one :country, through: :council end france = Country.create!(name: 'France') council = france.create_council! name: 'Council' council.members.create! name: 'Bob' council.members.create! name: 'Alice' council.members.create! name: 'Aaron', active: false class BugTest < Minitest::Test def test_historical_has_through_with_where_clause assert Country.as_of(Time.now).includes(:first_member).last end end ```