diff --git a/lib/chrono_model/patches/relation.rb b/lib/chrono_model/patches/relation.rb index dad976f8..23a6d0fb 100644 --- a/lib/chrono_model/patches/relation.rb +++ b/lib/chrono_model/patches/relation.rb @@ -17,6 +17,12 @@ def preload_associations(records) # :nodoc: end end + def empty_scope? + return super unless @_as_of_time + + @values == klass.as_of(as_of_time).values + end + def load return super unless @_as_of_time && !loaded? diff --git a/spec/chrono_model/history_models_spec.rb b/spec/chrono_model/history_models_spec.rb index 9f3b0468..c4813f50 100644 --- a/spec/chrono_model/history_models_spec.rb +++ b/spec/chrono_model/history_models_spec.rb @@ -15,6 +15,7 @@ expected['boos'] = Boo::History if defined?(Boo::History) expected['sub_bars'] = SubBar::History if defined?(SubBar::History) + expected['sub_sub_bars'] = SubSubBar::History if defined?(SubSubBar::History) # default_scope_spec expected['defoos'] = Defoo::History if defined?(Defoo::History) diff --git a/spec/chrono_model/time_machine/as_of_spec.rb b/spec/chrono_model/time_machine/as_of_spec.rb index 3c6655d1..9ea3d0bb 100644 --- a/spec/chrono_model/time_machine/as_of_spec.rb +++ b/spec/chrono_model/time_machine/as_of_spec.rb @@ -150,6 +150,8 @@ it { expect(Foo.as_of($t.subbar.ts[1]).includes(:bars, :sub_bars).first.sub_bars.first.name).to eq 'bar sub-bar' } it { expect(Foo.as_of($t.subbar.ts[2]).includes(:bars, :sub_bars).first.sub_bars.first.name).to eq 'sub-bar sub-bar' } it { expect(Foo.as_of($t.subbar.ts[3]).includes(:bars, :sub_bars).first.sub_bars.first.name).to eq 'new sub-bar' } + + it { expect(Foo.as_of(Time.now).includes(:bars, :sub_bars, :sub_sub_bars).first.sub_sub_bars.compact.size).to eq 1 } end it 'does not raise RecordNotFound when no history records are found' do diff --git a/spec/support/time_machine/structure.rb b/spec/support/time_machine/structure.rb index 1877772e..802c78bd 100644 --- a/spec/support/time_machine/structure.rb +++ b/spec/support/time_machine/structure.rb @@ -30,6 +30,7 @@ class ::Foo < ActiveRecord::Base has_many :bars has_many :sub_bars, through: :bars + has_many :sub_sub_bars, through: :sub_bars belongs_to :goo, class_name: 'FooGoo', optional: true end @@ -93,10 +94,22 @@ class ::SubBar < ActiveRecord::Base include ChronoModel::TimeMachine belongs_to :bar + has_many :sub_sub_bars has_timeline with: :bar end + adapter.create_table 'sub_sub_bars', temporal: true do |t| + t.string :name + t.references :sub_bar + end + + class ::SubSubBar < ActiveRecord::Base + include ChronoModel::TimeMachine + + belongs_to :sub_bar + end + adapter.create_table 'bazs' do |t| t.string :name t.references :bar @@ -126,6 +139,8 @@ class ::Baz < ActiveRecord::Base $t.subbar = ts_eval { SubBar.create! name: 'sub-bar', bar: $t.bar } ts_eval($t.subbar) { update! name: 'bar sub-bar' } + ts_eval { SubSubBar.create! name: 'sub-sub-bar', sub_bar: $t.subbar } + ts_eval($t.foo) { update! name: 'new foo' } ts_eval($t.bar) { update! name: 'bar bar' }