The following code raises NameError error, because the result variable is defined only in the transaction scope.
ActiveRecord::Base.transaction do
result = 1
end
result # NameError: undefined local variable or method `result'
To fix it I need:
- to define the
result variable before the transaction block
result = nil
ActiveRecord::Base.transaction do
result = 1
end
result
- to assign transaction block to the
result variable:
result =
ActiveRecord::Base.transaction do
1
end
- to replace local variable with instance variable:
ActiveRecord::Base.transaction do
@result = 1
end
@result
Which way is better?