This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the algorithms category.
Last Updated: 2024-11-21
I deployed the following "fix" to another bug, only to discover it also failed:
def tag_list=(names)
self.tags = names.split(',').map do |n|
next if n.blank?
Tag.where(name: n.strip).first_or_create!
end
end
With input "instruments, ", the second entry was nil. So effectively this code
boiled down to self.tags = [Tag<name: instrument>, nil]
But self.tags expects only tag entries. The nil cannot be handled. Kablam
When calling bare next
in a mapping loop, you need to anticipate nils
in the
output.