Skip to content

Commit ba02475

Browse files
committed
further work on #173
1 parent def6e8f commit ba02475

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

ruby/hyper-model/lib/reactive_record/active_record/reactive_record/isomorphic_base.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ def self.save_records(models, associations, acting_user, validate, save)
453453
elsif parent.class.reflect_on_association(association[:attribute].to_sym).collection?
454454
#puts ">>>>>>>>>> #{parent.class.name}.send('#{association[:attribute]}') << #{reactive_records[association[:child_id]]})"
455455
dont_save_list.delete(parent)
456+
457+
456458
if reactive_records[association[:child_id]]&.new_record?
457459
dont_save_list << reactive_records[association[:child_id]]
458460
end
@@ -465,14 +467,14 @@ def self.save_records(models, associations, acting_user, validate, save)
465467
else
466468
#puts ">>>>ASSOCIATION>>>> #{parent.class.name}.send('#{association[:attribute]}=', #{reactive_records[association[:child_id]]})"
467469
parent.send("#{association[:attribute]}=", reactive_records[association[:child_id]])
468-
469470
dont_save_list.delete(parent)
470-
if reactive_records[association[:child_id]]&.new_record? && parent.class.reflect_on_association(association[:attribute].to_sym).macro == :has_one
471+
472+
if parent.class.reflect_on_association(association[:attribute].to_sym).macro == :has_one &&
473+
reactive_records[association[:child_id]]&.new_record?
471474
dont_save_list << reactive_records[association[:child_id]]
472475
end
473476
end
474477
end if associations
475-
476478
# get rid of any records that don't require further processing, as a side effect
477479
# we also save any records that need to be saved (these may be rolled back later.)
478480

@@ -481,6 +483,7 @@ def self.save_records(models, associations, acting_user, validate, save)
481483
next true if record.frozen? # skip (but process later) frozen records
482484
next true if dont_save_list.include?(record) # skip if the record is on the don't save list
483485
next true if record.changed.include?(record.class.primary_key) # happens on an aggregate
486+
next true if record.persisted? # record may be have been saved as result of has_one assignment
484487
next false if record.id && !record.changed? # throw out any existing records with no changes
485488
# if we get to here save the record and return true to keep it
486489
op = new_models.include?(record) ? :create_permitted? : :update_permitted?
@@ -505,7 +508,6 @@ def self.save_records(models, associations, acting_user, validate, save)
505508
attributes[model.class.primary_key] = model[model.class.primary_key]
506509
[reactive_record_id, model.class.name, attributes, messages]
507510
end
508-
509511
# if we are not saving (i.e. just validating) then we rollback the transaction
510512

511513
raise ActiveRecord::Rollback, 'This Rollback is intentional!' unless save

ruby/hyper-model/spec/batch2/one_to_one_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def compare_to_server(model, expression, expected_result, load=true)
8585
end
8686

8787
before(:each) do
88+
Parent.delete_all
89+
Child.delete_all
8890
@parent1 = Parent.create(name: 'parent1')
8991
@child1 = Child.create(name: 'child1', parent: @parent1)
9092
end
@@ -96,4 +98,12 @@ def compare_to_server(model, expression, expected_result, load=true)
9698
it 'will load the child' do
9799
compare_to_server @parent1, 'child.name', 'child1'
98100
end
101+
102+
it 'saving parent and child from client' do
103+
expect_promise do
104+
Parent.new(name: 'parent', child: Child.new(name: 'child')).save.then do |response|
105+
response[:saved_models].collect { |m| m[1] }
106+
end
107+
end.to contain_exactly('Parent', 'Child')
108+
end
99109
end

0 commit comments

Comments
 (0)