Ruby on rails 在RubyonRails中保存模型后如何回滚?
我使用RubyonRails3.2编写了这段代码Ruby on rails 在RubyonRails中保存模型后如何回滚?,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,我使用RubyonRails3.2编写了这段代码 def craete @account = Account.new(account_params) if @account.save @account = Account.where(name: params[:name]).first @tenant = Tenant.new() @tenant.name = @account.name @tenant.account_id = @ac
def craete
@account = Account.new(account_params)
if @account.save
@account = Account.where(name: params[:name]).first
@tenant = Tenant.new()
@tenant.name = @account.name
@tenant.account_id = @account.id
if !@tenant.save
raise ApiError.new(ApiError::VALIDATION, @tenant.errors, 1103, :unprocessable_entity)
end
audit('Admin.Account.Create', account_params)
@success = ApiSuccess.new(@account, 1210, request.method, false, 'Account created')
render '/api_success', status: :created
else
raise ApiError.new(ApiError::VALIDATION, @account.errors, 1103, :unprocessable_entity)
end
end
我的问题是,如果租户无法保存,我们如何回滚帐户
非常感谢您使用此功能。在事务块下保存帐户和租户。如果其中任何一个失败,整个事务将回滚
@account.transaction do
if @account.save
@account = Account.where(name: params[:name]).first
@tenant = Tenant.new()
@tenant.name = @account.name
@tenant.account_id = @account.id
if !@tenant.save
raise ApiError.new(ApiError::VALIDATION, @tenant.errors, 1103, :unprocessable_entity)
end
audit('Admin.Account.Create', account_params)
success = ApiSuccess.new(@account, 1210, request.method, false, 'Account created')
render '/api_success', status: :created
else
raise ApiError.new(ApiError::VALIDATION, @account.errors, 1103, :unprocessable_entity)
end
结束{“social.audit”:{“auditable_type”:“Admin.Account.Create”,“customer_id”:1,“audited_changes”:{“name”:“test”,“email”:test@yahoo.com“}”,created_at:“2014-07-26T18:26:29+07:00”,“action:“Admin.Account.Create”}已返回!回复\代码=312,回复\文本=NO\路由,无法保存事件I输入的正确数据?