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