Rails 控制器 - 仅当内部的两个方法成功时才执行操作(相互依赖的方法) [英] Rails controller - execute action only if the two methods inside succeed (mutually dependent methods)

查看:32
本文介绍了Rails 控制器 - 仅当内部的两个方法成功时才执行操作(相互依赖的方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,有一个名为actions"的方法.当应用程序进入方法example_action"时,它会实现第一个方法 (1) update_user_table,然后 (2) 另一个 update_userdeal_table.(都会读写数据库)

I have a Controller with a method called 'actions'. When the app goes inside the method 'example_action', it implements a first method (1) update_user_table and then (2) another update_userdeal_table. (both will read and write database)

我的问题如下:在控制器中间超时的情况下,我想避免更新用户表(通过方法 1)但未更新 UserDeal 表(通过方法 2)的情况).在我的应用程序中,对于移动用户,如果他们在有互联网连接的地铁中,他们会启动通过example_action"控制器的请求,成功执行第一种方法(1),但随后他们进入隧道 60 秒,非常非常低(<5b/sec)或没有互联网连接,因此出于 UX 原因,我将请求超时并向用户显示抱歉太长,再试一次".问题是损坏"已经在数据库中了:) => (1) 已经执行但没有 (2).在我的应用程序中,如果 Userdeal 未更新,则完全不可能更新 User 表(这会造成数据一致性问题...)

My issue is the following: in case of timeout in the middle of the controller, I want to avoid the case where the User table (via method 1) is updated but the UserDeal table is NOT (via method 2). In my app, for mobile users if they're in a subway with internet connection, they launch the request that goes through 'example_action' controller, performs successfully the first method (1) but then they enter a tunnel for 60 seconds with very very low (<5b/sec) or NO internet connection, so for UX reasons, I timeout the request and display to the user 'sorry too long, try again'. The problem is that the "damage" is already here in the database:) => (1) was already performed but not (2). And in my app it is totally out of question to update the User table if the Userdeal was not updated (it would create data consistency issues...)

我需要两种方法(1)和(2)相互依赖":如果一个不成功,另一个不应该被执行.这是我能描述它的最好方式.

I need the two methods (1) and(2) to be "mutually dependent": if one does not succeed, the other one should not be performed. It's the best way I can describe it.

在实践中,由于 (1) 首先发生,如果 (1) 失败,则不会执行 (2).完美.

In practice, as (1) happens first, if (1) fails, then (2) won't be performed. Perfect.

问题是如果 (1) 成功而 (2) 没有执行.我怎么能对 Rails 说,如果 (2) 没有成功执行,那么我不想执行块 'example_action' 内的任何事情.

The problem is if (1) succeeds and (2) is not performed. How can I say to Rails, if (2) is not performed successfully, then I don't want to execute any of the things inside the block 'example_action'.

这可能吗?

class DealsController < ApplicationController
  def example_action
    update_user_table
    update_userdeal_table   
  end

  private
  def update_user_table
    # update the table User so it needs to connect to internet and acces the distant User table
  end
  def update_userdeal_table
    # update the table UserDeal table so it needs to connect to internet and access the distant UserDeal table
  end
end

推荐答案

如果您正在使用 ActiveRecord,您可以将您的方法移动到模型中并在 transaction 阻止.

If you are using ActiveRecord, you could move your methods into the model and perform them in a transaction block.

class DealsController < ApplicationController
  def example_action
      // 'user' would have to be defined, or you could work with it as a class method in some way
      user.make_the_deal
  end
end

class User < ActiveRecord::Base
  def make_the_deal
    transaction do
      update_user_table
      update_userdeal_table
    end
  end

  def update_user_table
  end

  def update_userdeal_table
  end
end

你不一定要放入你的模型来做到这一点,你可以这样做:

You don't necessarily have to put in your model to do this, you can just do:

User.transaction do
  update_user_table
  update_userdeal_table
end

在您的控制器中.但建议将事务放入模型中.

in your controller. But it's recommended to put transactions in the model.

这篇关于Rails 控制器 - 仅当内部的两个方法成功时才执行操作(相互依赖的方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆