Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ydakuka opened this issue Jun 21, 2023 · 0 comments
Open

Comments

@ydakuka
Copy link

ydakuka commented Jun 21, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant