mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ignore strict loading violations on instances loaded through fixtures
This commit is contained in:
parent
ef61c9c8a3
commit
e100c398db
6 changed files with 37 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
2
activerecord/test/fixtures/strict_zines.yml
vendored
Normal file
2
activerecord/test/fixtures/strict_zines.yml
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
going_out:
|
||||
title: Hello
|
7
activerecord/test/models/strict_zine.rb
Normal file
7
activerecord/test/models/strict_zine.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "models/zine"
|
||||
|
||||
class StrictZine < Zine
|
||||
self.strict_loading_by_default = true
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue