本地变量
begin
transaction #Code inside transaction
object = Class.new attributes
raise unless object.save!
end
rescue
puts object.error.full_messages # Why can't we use local varible inside rescue ?
end
实例变量
begin
transaction #Code inside transaction
@object = Class.new attributes
raise unless @object.save!
end
rescue
puts @object.error.full_messages # This is working fine.
end
答案 0 :(得分:28)
您当然可以在相应的begin
块中访问rescue
中定义的局部变量(当然,假设在设置变量之后引发了异常)。
您不能做的是访问块外部块内定义的局部变量。这与例外无关。看到这个简单的例子:
define transaction() yield end
transaction do
x = 42
end
puts x # This will cause an error because `x` is not defined here.
你可以做些什么来解决这个问题,就是在块之前定义变量(你可以将它设置为nil),然后在块中设置它。
x = nil
transaction do
x = 42
end
puts x # Will print 42
因此,如果您更改此代码,它将起作用:
begin
object = nil
transaction do #Code inside transaction
object = Class.new attributes
raise unless object.save!
end
rescue
puts object.error.full_messages # Why can't we use local varible inside rescue ?
end
答案 1 :(得分:0)
您可以从 ActiveRecord::RecordInvalid 中获取错误消息。可以在此处的官方文档示例中看到。
https://api.rubyonrails.org/v6.1.3.2/classes/ActiveRecord/RecordInvalid.html
begin
transaction
Class.create! attributes
end
rescue ActiveRecord::RecordInvalid => invalid
puts invalid.record.error.full_messages
end