Skip to content

Suggestion: Add notes about returned value of ActiveRecord transaction #338

Open
@ydakuka

Description

@ydakuka

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:

  1. to define the result variable before the transaction block
result = nil

ActiveRecord::Base.transaction do
  result = 1
end

result
  1. to assign transaction block to the result variable:
result =
  ActiveRecord::Base.transaction do
    1
  end
  1. to replace local variable with instance variable:
ActiveRecord::Base.transaction do
  @result = 1
end

@result

Which way is better?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions