Skip to content

Commit 2076c74

Browse files
nhasselmeyerkratob
authored andcommitted
Fix record lookup when id is given as string
1 parent 0777648 commit 2076c74

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/active_type/nested_attributes/association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def derive_class_name
9797
end
9898

9999
def fetch_child(parent, id)
100-
assigned = assigned_children(parent).detect { |r| r.id == id }
100+
assigned = assigned_children(parent).detect { |r| r.id.to_s == id.to_s }
101101
return assigned if assigned
102102

103103
if child = find_scope(parent).find_by_id(id)

spec/active_type/nested_attributes_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,19 @@ def should_assign_and_persist(assign, persist = assign)
235235
should_assign_and_persist(["existing 1", "updated", "existing 3"])
236236
end
237237

238+
it 'allows the id to be given as a string' do
239+
subject.records = [
240+
NestedAttributesSpec::Record.new(:persisted_string => "existing 1"),
241+
NestedAttributesSpec::Record.new(:persisted_string => "existing 2"),
242+
]
243+
subject.records[0].id = 100
244+
subject.records[1].id = 101
245+
246+
subject.records_attributes = { '1' => { 'id' => '101', 'persisted_string' => 'updated' } }
247+
248+
should_assign_and_persist(["existing 1", "updated"])
249+
end
250+
238251
it 'does not update records matching a reject_if proc' do
239252
extra_options.merge!(:reject_if => :bad)
240253
subject.records = [

0 commit comments

Comments
 (0)