Reputation: 3398
I'm using ruby on rails for a project and noticed I'm using some pattern on my functions that goes like this:
begin
ActiveRecord::Base.transaction do
#some database manipulation
end
return true
rescue
return false
end
So I would like to DRY my code, in a way that only that #some database manipulation is gonna change. Any ideas of how I could do that? Thanks in advance!
Upvotes: 0
Views: 91
Reputation: 659
You could create a method like this:
def with_transaction
ActiveRecord::Base.transaction do
yield
end
return true
rescue
return false
end
And then use it like:
with_transaction do
# some db manipulation
end
But generally I would advise against silencing errors like that.
Upvotes: 3