From e100c398dba995283d8d63fb9df38b5b885053ff Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Thu, 10 Dec 2020 14:00:40 -0600 Subject: [PATCH] Ignore strict loading violations on instances loaded through fixtures --- activerecord/lib/active_record/fixtures.rb | 5 ++++- .../test/cases/strict_loading_test.rb | 19 +++++++++++++++++++ activerecord/test/fixtures/strict_zines.yml | 2 ++ activerecord/test/models/strict_zine.rb | 7 +++++++ activerecord/test/models/zine.rb | 2 +- activerecord/test/schema/schema.rb | 4 ++++ 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 activerecord/test/fixtures/strict_zines.yml create mode 100644 activerecord/test/models/strict_zine.rb diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index f2257be306..6224f20abf 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -773,9 +773,12 @@ module ActiveRecord def find raise FixtureClassNotFound, "No class attached to find." unless model_class - model_class.unscoped do + object = model_class.unscoped do model_class.find(fixture[model_class.primary_key]) end + # Fixtures can't be eagerly loaded + object.instance_variable_set(:@strict_loading, false) + object end end end diff --git a/activerecord/test/cases/strict_loading_test.rb b/activerecord/test/cases/strict_loading_test.rb index c730fbe588..18212dc3aa 100644 --- a/activerecord/test/cases/strict_loading_test.rb +++ b/activerecord/test/cases/strict_loading_test.rb @@ -6,6 +6,8 @@ require "models/computer" require "models/mentor" require "models/project" require "models/ship" +require "models/strict_zine" +require "models/interest" class StrictLoadingTest < ActiveRecord::TestCase fixtures :developers @@ -417,3 +419,20 @@ class StrictLoadingTest < ActiveRecord::TestCase end end end + +class StrictLoadingFixturesTest < ActiveRecord::TestCase + fixtures :strict_zines + + test "strict loading violations are ignored on fixtures" do + ActiveRecord::FixtureSet.reset_cache + create_fixtures("strict_zines") + + assert_nothing_raised do + strict_zines(:going_out).interests.to_a + end + + assert_raises(ActiveRecord::StrictLoadingViolationError) do + StrictZine.first.interests.to_a + end + end +end diff --git a/activerecord/test/fixtures/strict_zines.yml b/activerecord/test/fixtures/strict_zines.yml new file mode 100644 index 0000000000..167b3f8bbd --- /dev/null +++ b/activerecord/test/fixtures/strict_zines.yml @@ -0,0 +1,2 @@ +going_out: + title: Hello diff --git a/activerecord/test/models/strict_zine.rb b/activerecord/test/models/strict_zine.rb new file mode 100644 index 0000000000..24f66af15b --- /dev/null +++ b/activerecord/test/models/strict_zine.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require "models/zine" + +class StrictZine < Zine + self.strict_loading_by_default = true +end diff --git a/activerecord/test/models/zine.rb b/activerecord/test/models/zine.rb index 6f361665ef..fb4519e289 100644 --- a/activerecord/test/models/zine.rb +++ b/activerecord/test/models/zine.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class Zine < ActiveRecord::Base - has_many :interests, inverse_of: :zine + has_many :interests, inverse_of: :zine, foreign_key: "zine_id" end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 6292a0473c..02b75a24e4 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -1101,6 +1101,10 @@ ActiveRecord::Schema.define do t.string :title end + create_table :strict_zines, force: true do |t| + t.string :title + end + create_table :wheels, force: true do |t| t.integer :size t.references :wheelable, polymorphic: true