unscoped can take a block

This commit is contained in:
Dylan Knutson
2025-07-17 21:41:16 +00:00
parent d6792cebeb
commit d3d459da95
3 changed files with 13 additions and 6 deletions

View File

@@ -21,11 +21,10 @@ module HasAuxTable
sig { params(main_model: ActiveRecord::Base).returns(ActiveRecord::Base) }
def ensure_aux_target(main_model)
aux_association = main_model.association(self.aux_association_name)
# binding.pry if main_model.persisted? && !aux_association.loaded?
aux_association.target ||=
(
if main_model.persisted?
aux_association.load_target
aux_association.load_target || aux_association.build
else
aux_association.build
end

View File

@@ -38,10 +38,12 @@ module HasAuxTable
main_class,
:unscoped,
false
) do |original, *args, **kwargs|
original.call(*args, **kwargs).eager_load(
aux_config.aux_association_name
)
) do |original, *args, **kwargs, &block|
ret = original.call(*args, **kwargs, &block)
if ret.is_a?(ActiveRecord::Relation)
ret.eager_load!(aux_config.aux_association_name)
end
ret
end
relation_class =

View File

@@ -788,6 +788,12 @@ RSpec.describe HasAuxTable do
expect(boat.only_freshwater).to eq(true)
end
it "unscoped can take a block" do
car = Car.create!(name: "Honda Civic", fuel_type: "gasoline")
car = Car.unscoped { Car.find(car.id) }
expect(car.fuel_type).to eq("gasoline")
end
describe "namespaced models" do
it "works with namespaced models" do
fork1 =