Ruby on rails 实例变量定义在方法()调用后消失
我有两个工作,一个打电话给另一个。第一个活动工单完成并调用第二个活动工单,为其提供在执行后完成的方法。我运行了一个调试器,发现在方法('ending_method')之前的所有内容都已被删除。调用返回helpers just find。在method.call之后,只有第一个助手正确返回,第二个助手返回nil,但是如果我手动执行第一个_helper.attribute,则返回值。这是为什么??我认为这与变量作用域有关,但由于它不一致,这让我感到困惑 活动工单1:Ruby on rails 实例变量定义在方法()调用后消失,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我有两个工作,一个打电话给另一个。第一个活动工单完成并调用第二个活动工单,为其提供在执行后完成的方法。我运行了一个调试器,发现在方法('ending_method')之前的所有内容都已被删除。调用返回helpers just find。在method.call之后,只有第一个助手正确返回,第二个助手返回nil,但是如果我手动执行第一个_helper.attribute,则返回值。这是为什么??我认为这与变量作用域有关,但由于它不一致,这让我感到困惑 活动工单1: class FirstJob &
class FirstJob < ApplicationJob
include Helper
queue_as :default
before_perform do |obj|
first_helper(obj.arguments.first)
end
def perform(model_id,length)
code....
other_method
end
def other_method
second_helper ----> works fine
end
我在排除过程后发现了这一点,它与方法()调用无关。问题如下。“if”语句中的代码(尽管为false且从未解析)将第二个_helper变量覆盖为nil。我意识到,事后无论如何我都不应该这样做,因为我没有设置对象属性,而是设置一个变量。不知道为什么会出现这种情况。。。我觉得很奇怪
if (false)
second_helper += 1
end
def second_helper
return @second_helper if defined?(@second_helper)
@second_helper = first_helper.attribute
end
SecondJob
中执行之前的是否应该调用first\u helper
?而FirstJob
中的helper\u方法也应该做同样的事情吗?对不起,我打错了。。。他们都称同一件事为第一助手。我现在就纠正它。这似乎不是一个实际的答案。你澄清了事情(这很好!),但你也提出了更多的问题。也许你应该更新原始问题。
module Helper
def first_helper(*id_string)
return @first_helper if defined?(@first_helper)
@first_helper = Model.find_by(model_id: id_string)
end
def second_helper
return @second_helper if defined?(@second_helper)
@second_helper = first_helper.attribute
end
if (false)
second_helper += 1
end
def second_helper
return @second_helper if defined?(@second_helper)
@second_helper = first_helper.attribute
end