Watch out for Ruby blocks scope gotcha

def update
availability.update(availability_params)
metrics_tracker = MetricsTracker.build_for(activity)
metrics_tracker.save
if metrics_tracker.trigger_notification?
# send email to admin
end
# rendering code
end
# code omitted...def metrics_tracker
@metrics_tracker ||= MetricsTracker.new(current_user)
end
def update
ApplicationRecord.transaction do # <= added transaction here
availability.update!(availability_params)
metrics_tracker = MetricsTracker.build_for(activity)
metrics_tracker.save!
end
if metrics_tracker.trigger_notification?
# send email to admin
end
# rendering code
end
ApplicationRecord.transaction do
availability.update!(availability_params)
metrics_tracker = MetricsTracker.build_for(activity)
metrics_tracker.save!
end
def update
ApplicationRecord.transaction do
availability.update!(availability_params)
@metrics_tracker = MetricsTracker.build_for(activity) # <= here!
metrics_tracker.save!
end
if metrics_tracker.trigger_notification?
# send email to admin
end
# rendering code
end
# code omitted...def metrics_tracker
@metrics_tracker ||= MetricsTracker.new(current_user)
end

--

--

Ru(g)by fan, Ruby on Rails / Javascript freelancer, Haskell lover

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
benjamin roth

benjamin roth

409 Followers

Ru(g)by fan, Ruby on Rails / Javascript freelancer, Haskell lover