在处理Rails应用程序并调查错误时,我将其缩小到以下情况,其中行AAA将导致events
返回nil,在点XXX或YYYY之后不运行代码,但是行BBB工作正如所料。我每次都来回切换几次干净的环境,它总是表现得很奇怪。
Ruby 2.5.0,尚未尝试降级但我怀疑这个问题出现在2.3中(我使用此应用程序跳过了2.4)。
# this is the method that has the issue when called. It's in a PORO service object instantiated in a helper method & called from a Rails view.
def events
@events ||= begin
events = []
# some code building up events
events << :whatever if fulfillment.ready_to_process?
#XXX -- execution never reaches here with #AAA. But no error is thrown, and events returns nil
# some more code building up events
end
end
# Fulfillment & LineItem are ActiveRecords
class Fulfillment
def ready_to_process?
# some "return false if" assertions
result = line_items.any?(&:fulfillment_needed?) #AAA
result = line_items.any?{ |line_item| line_item.fulfillment_needed? } #BBB
# YYY -- exececution never reaches here with #AAA
result
end
end
class LineItem
def fulfillment_needed?
# various assertions
true
end
end
编辑:请注意,在出现错误的上下文中,fulfillment_needed?总是如此。
编辑2:实际上,我发现了另外一件遇到的事情?可以切换错误:
class LineItem
has_many :vendor_line_items, dependent: :destroy
def fulfillment_needed?
return true if vendor_line_items.empty? # Displays the bug, AAA fails but BBB works
return true # Does not display the bug AAA && BBB work
end
end
vendor_line_items.empty?
通常在其他地方表现。