tests for has_many

This commit is contained in:
Dylan Knutson
2025-07-24 05:22:55 +00:00
parent ad8ae7945b
commit 4249329fa3
3 changed files with 78 additions and 11 deletions

View File

@@ -140,6 +140,7 @@ module HasAuxTable
if self.aux.column_names.include?(k.to_s)
# attribute is a column on the aux table
aux[k] = v
next
elsif assoc = self.main.klass.reflect_on_association(k.to_s)
if assoc.is_a?(ActiveRecord::Reflection::BelongsToReflection)
# attribute is an association on the main class
@@ -148,25 +149,23 @@ module HasAuxTable
# the association is a column on the aux table, `v` is
# a model, get the primary key of the model
aux[fk] = v && v.send(assoc.association_primary_key)
else
# association is on the main table, `v` is a model,
main[k] = v
next
end
elsif assoc.is_a?(ActiveRecord::Reflection::HasOneReflection)
elsif assoc.is_a?(ActiveRecord::Reflection::HasOneReflection) ||
assoc.is_a?(ActiveRecord::Reflection::HasManyReflection)
pk = assoc.active_record_primary_key
if self.aux.column_names.include?(pk)
aux[pk] = v && v.send(assoc.foreign_key)
else
main[k] = v
next
end
else
main[k] = v
raise "unsupported association type: #{assoc.class}"
end
else
# attribute is not a column on the aux table or an association,
# assume it's a column on the main table
main[k] = v
end
# attribute is not a column on the aux table or an association,
# assume it's a column on the main table
main[k] = v
end
[main, aux]
end

View File

@@ -894,6 +894,54 @@ RSpec.describe HasAuxTable do
expect(ModelECustom.where(owned_aux_to_aux: @e1)).to eq([])
end
end
describe "has_many association" do
it "works between base and base" do
# e1.pk_base_id -> e2.fk_base_id
@e1.owned_base_to_base_many << @e2
@e1.save!
@e2.save!
expect(@e1.pk_base_id).to eq(1)
expect(@e2.fk_base_id).to eq(1)
expect(ModelECustom.where(owned_base_to_base_many: @e2)).to eq(
[@e1]
)
expect(ModelECustom.where(owned_base_to_base_many: @e1)).to eq([])
end
it "works between base and aux" do
# e1.pk_base_id -> e2.fk_aux_id
@e1.owned_base_to_aux_many << @e2
@e1.save!
@e2.save!
expect(@e1.pk_base_id).to eq(1)
expect(@e2.fk_aux_id).to eq(1)
expect(ModelECustom.where(owned_base_to_aux_many: @e2)).to eq([@e1])
expect(ModelECustom.where(owned_base_to_aux_many: @e1)).to eq([])
end
it "works between aux and base" do
# e1.pk_aux_id -> e2.fk_base_id
@e1.owned_aux_to_base_many << @e2
@e1.save!
@e2.save!
expect(@e1.pk_aux_id).to eq(3)
expect(@e2.fk_base_id).to eq(3)
expect(ModelECustom.where(owned_aux_to_base_many: @e2)).to eq([@e1])
expect(ModelECustom.where(owned_aux_to_base_many: @e1)).to eq([])
end
it "works between aux and aux" do
# e1.pk_aux_id -> e2.fk_aux_id
@e1.owned_aux_to_aux_many << @e2
@e1.save!
@e2.save!
expect(@e1.pk_aux_id).to eq(3)
expect(@e2.fk_aux_id).to eq(3)
expect(ModelECustom.where(owned_aux_to_aux_many: @e2)).to eq([@e1])
expect(ModelECustom.where(owned_aux_to_aux_many: @e1)).to eq([])
end
end
end
end

View File

@@ -465,4 +465,24 @@ class ModelECustom < ModelE
class_name: "ModelECustom",
primary_key: :pk_aux_id,
foreign_key: :fk_aux_id
has_many :owned_base_to_base_many,
class_name: "ModelECustom",
primary_key: :pk_base_id,
foreign_key: :fk_base_id
has_many :owned_base_to_aux_many,
class_name: "ModelECustom",
primary_key: :pk_base_id,
foreign_key: :fk_aux_id
has_many :owned_aux_to_base_many,
class_name: "ModelECustom",
primary_key: :pk_aux_id,
foreign_key: :fk_base_id
has_many :owned_aux_to_aux_many,
class_name: "ModelECustom",
primary_key: :pk_aux_id,
foreign_key: :fk_aux_id
end