Skip to content

Commit d859da0

Browse files
kbrockclaude
andcommitted
Fix array format compatibility and nil handling
- Guard validator against nil ancestor_ids (array format, no serializer) - Coerce nil to [] in ancestor_ids setter for NOT NULL columns - Only define before_type_cast ancestry reader for string formats (array format has no serializer, no string conversion needed) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 528c81a commit d859da0

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

lib/ancestry/ancestry_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Ancestry
44
class AncestryValidator < ActiveModel::EachValidator
55
def validate_each(record, attribute, value)
6-
return if value.empty?
6+
return if value.nil? || value.empty?
77

88
if options[:integer]
99
unless value.all? { |id| id.kind_of?(Integer) && id > 0 }

lib/ancestry/instance_methods_builder.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,24 @@ def ancestors?
4343
alias has_parent? ancestors?
4444

4545
def ancestor_ids=(value)
46+
value = value.presence || []
4647
write_attribute(:#{column}, value)
4748
#{"ancestry_sync_parent_cache(#{parent_cache_column.inspect}, value)" if parent_cache_column || parent_association}
4849
#{"ancestry_sync_root_cache(#{root_cache_column.inspect}, value)" if root_cache_column || root_association}
4950
end
5051

51-
# backwards compatibility: ancestry returns the raw string
52-
def #{column}
53-
read_attribute_before_type_cast(:#{column})
54-
end
52+
#{ if format_module.build_serializer(integer_pk: integer_pk)
53+
<<~RUBY
54+
# backwards compatibility: ancestry returns the raw string
55+
def #{column}
56+
read_attribute_before_type_cast(:#{column})
57+
end
5558
56-
def #{column}=(value)
57-
super(value)
58-
end
59+
def #{column}=(value)
60+
super(value)
61+
end
62+
RUBY
63+
end}
5964

6065
def ancestor_ids
6166
read_attribute(:#{column})

0 commit comments

Comments
 (0)