loading through base class
This commit is contained in:
@@ -21,7 +21,15 @@ 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)
|
||||
aux_association.target ||= aux_association.build
|
||||
# binding.pry if main_model.persisted? && !aux_association.loaded?
|
||||
aux_association.target ||=
|
||||
(
|
||||
if main_model.persisted?
|
||||
aux_association.load_target
|
||||
else
|
||||
aux_association.build
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
sig do
|
||||
|
||||
@@ -38,7 +38,7 @@ RSpec.describe HasAuxTable do
|
||||
end
|
||||
|
||||
t.create_aux :boat do |t|
|
||||
t.string :boat_type
|
||||
t.boolean :only_freshwater
|
||||
end
|
||||
end
|
||||
|
||||
@@ -771,6 +771,23 @@ RSpec.describe HasAuxTable do
|
||||
end
|
||||
end
|
||||
|
||||
it "loads the aux data separately when loaded from main class" do
|
||||
car =
|
||||
Car.create!(name: "Honda Civic", fuel_type: "gasoline", engine_size: 2.0)
|
||||
boat = Boat.create!(name: "Yacht", only_freshwater: true)
|
||||
expect(Vehicle.count).to eq(2)
|
||||
expect(Car.count).to eq(1)
|
||||
expect(Boat.count).to eq(1)
|
||||
|
||||
expect(count_queries { car = Vehicle.find(car.id) }).to eq(1)
|
||||
expect(car.fuel_type).to eq("gasoline")
|
||||
expect(car.engine_size).to eq(2.0)
|
||||
expect(car.name).to eq("Honda Civic")
|
||||
|
||||
expect(count_queries { boat = Vehicle.find(boat.id) }).to eq(1)
|
||||
expect(boat.only_freshwater).to eq(true)
|
||||
end
|
||||
|
||||
describe "namespaced models" do
|
||||
it "works with namespaced models" do
|
||||
fork1 =
|
||||
|
||||
Reference in New Issue
Block a user