From 4249329fa3a6853e5ce75d219cc877755dbf85e1 Mon Sep 17 00:00:00 2001 From: Dylan Knutson Date: Thu, 24 Jul 2025 05:22:55 +0000 Subject: [PATCH] tests for has_many --- lib/has_aux_table/aux_table_config.rb | 21 ++++++------ spec/has_aux_table_spec.rb | 48 +++++++++++++++++++++++++++ spec/spec_models.rb | 20 +++++++++++ 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/lib/has_aux_table/aux_table_config.rb b/lib/has_aux_table/aux_table_config.rb index 1550e7b..038947a 100644 --- a/lib/has_aux_table/aux_table_config.rb +++ b/lib/has_aux_table/aux_table_config.rb @@ -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 diff --git a/spec/has_aux_table_spec.rb b/spec/has_aux_table_spec.rb index 884603e..a3cc7d0 100644 --- a/spec/has_aux_table_spec.rb +++ b/spec/has_aux_table_spec.rb @@ -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 diff --git a/spec/spec_models.rb b/spec/spec_models.rb index b0fbf9c..b45ee6c 100644 --- a/spec/spec_models.rb +++ b/spec/spec_models.rb @@ -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