From 9355151bbeadbdb289b0249c047002f28b8d1eb1 Mon Sep 17 00:00:00 2001 From: Giallombardo Nathan Date: Tue, 28 May 2024 15:31:50 +0200 Subject: [PATCH] add reset association --- lib/couchbase-orm/associations.rb | 8 ++++++++ lib/couchbase-orm/utilities/has_many.rb | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/lib/couchbase-orm/associations.rb b/lib/couchbase-orm/associations.rb index 71514d5b..c15ac66b 100644 --- a/lib/couchbase-orm/associations.rb +++ b/lib/couchbase-orm/associations.rb @@ -53,6 +53,10 @@ def belongs_to(name, **options) instance_variable_set(instance_var, value) end + + define_method(:"#{name}_reset") do + remove_instance_variable(instance_var) if instance_variable_defined?(instance_var) + end end def has_and_belongs_to_many(name, **options) @@ -106,6 +110,10 @@ def has_and_belongs_to_many(name, **options) instance_variable_set(instance_var, value) end + define_method(:"#{name}_reset") do + self.remove_instance_variable(instance_var) if self.instance_variable_defined?(instance_var) + end + return unless options[:autosave] save_method = :"autosave_associated_records_for_#{name}" diff --git a/lib/couchbase-orm/utilities/has_many.rb b/lib/couchbase-orm/utilities/has_many.rb index 164b75f0..ce14d003 100644 --- a/lib/couchbase-orm/utilities/has_many.rb +++ b/lib/couchbase-orm/utilities/has_many.rb @@ -58,6 +58,10 @@ class #{class_name} < CouchbaseOrm::Base end end + define_method(:"#{model}_reset") do + self.remove_instance_variable(instance_var) if self.instance_variable_defined?(instance_var) + end + @associations ||= [] @associations << [model, options[:dependent]] end