1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #40792 from ghiculescu/fixture-strict-loading

Ignore strict loading violations on instances loaded through fixtures
This commit is contained in:
Eileen M. Uchitelle 2020-12-10 20:03:00 -05:00 committed by GitHub
commit 5cb6338540
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 2 deletions

View file

@ -773,9 +773,12 @@ module ActiveRecord
def find def find
raise FixtureClassNotFound, "No class attached to find." unless model_class 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]) model_class.find(fixture[model_class.primary_key])
end end
# Fixtures can't be eagerly loaded
object.instance_variable_set(:@strict_loading, false)
object
end end
end end
end end

View file

@ -6,6 +6,8 @@ require "models/computer"
require "models/mentor" require "models/mentor"
require "models/project" require "models/project"
require "models/ship" require "models/ship"
require "models/strict_zine"
require "models/interest"
class StrictLoadingTest < ActiveRecord::TestCase class StrictLoadingTest < ActiveRecord::TestCase
fixtures :developers fixtures :developers
@ -438,3 +440,20 @@ class StrictLoadingTest < ActiveRecord::TestCase
end end
end 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

View file

@ -0,0 +1,2 @@
going_out:
title: Hello

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
require "models/zine"
class StrictZine < Zine
self.strict_loading_by_default = true
end

View file

@ -1,5 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
class Zine < ActiveRecord::Base class Zine < ActiveRecord::Base
has_many :interests, inverse_of: :zine has_many :interests, inverse_of: :zine, foreign_key: "zine_id"
end end

View file

@ -1101,6 +1101,10 @@ ActiveRecord::Schema.define do
t.string :title t.string :title
end end
create_table :strict_zines, force: true do |t|
t.string :title
end
create_table :wheels, force: true do |t| create_table :wheels, force: true do |t|
t.integer :size t.integer :size
t.references :wheelable, polymorphic: true t.references :wheelable, polymorphic: true