我们有葡萄 API,但我想知道我们是否可以在每次请求时用活动记录事务包装它。
在进行交易的活动记录中,我们可以这样做:
ActiveRecord::Base.transaction do
# do select
# do update
# do insert
end
如何将它包装到葡萄 API 中?
据我所知,在 Grape API 中,我们可以实现before方法和after方法。
class API < Grape::API
before do
# ????? Need to implement code here to begin active record transaction
# this suppose to begin active record transaction
end
after do
# ????? Need to implement code here to end active record transaction
# this suppose to end active record transaction
end
end