事务无法回滚超出某些模型

时间:2015-05-17 12:12:18

标签: ruby-on-rails model transactions rescue

我必须使用多个模型处理很长的表格。

def registerCandidate
    action_redirect = ""
    id = ""
    ActiveRecord::Base.transaction do
        begin

        @entity = Entity.new( name: params[:entity][:name], description: params[:entity][:description], logo: params[:entity][:logo])
        @access = Access.new( username: params[:access][:username], password: params[:access][:password], remember_me: params[:access][:rememberme], password_confirmation: params[:access][:password_confirmation])
        @access.entity = @entity
        @access.save!


        @biodata = Biodatum.new(
            date_of_birth: params[:biodatum][:birthday],
            height: params[:biodatum][:height],
            family_members: params[:biodatum][:family_members],
            gender: params[:biodatum][:gender],
            complexion: params[:biodatum][:complexion],
            marital_status: params[:biodatum][:marital_status],
            blood_type: params[:biodatum][:blood_type],
            religion: params[:biodatum][:religion],
            education: params[:biodatum][:education],
            career_experience: params[:biodatum][:career_experience],
            notable_accomplishments: params[:biodatum][:notable_accomplishments],
            emergency_contact: params[:biodatum][:emergency_contact],
            languages_spoken: params[:biodatum][:languages_spoken]
        )
        @biodata.entity = @entity
        @biodata.save!

        @employee = Employee.new()
        @employee.entity = @entity
        @employee.save!

        action_redirect = "success_candidate_registration"
        id = @access.id
        #action_redirect = "success_candidate_registration?id=" + @access.id

        #Error Processing
        rescue StandardError => e
          flash[:collective_errors] = "An error of type #{e.class} happened, message is #{e.message}"
          action_redirect = "candidate_registration"
      end
    end
    redirect_to action: action_redirect, access_id: id
  end

如果我提出超出@access.save!的任何错误,它会在没有回滚的情况下执行整个事务。如何修改与所有模型相关的任何错误以回滚所有内容?

1 个答案:

答案 0 :(得分:0)

由于您正在拯救StandardError - 大多数错误都源自您,因此很可能吞下可能导致回滚的错误。

这通常被称为口袋妖怪异常处理(Gotta catch em' all),并且是一种反模式。

而是侦听更具体的错误,例如ActiveRecord::RecordInvalid - 并在出错时引发raise ActiveRecord::Rollback以导致回滚。

如果您还没有阅读Active Record Transactions docs,那么有一些非常好的信息可以说明会导致回滚的原因。

此外,如果你想成为一名优秀的红宝石公民,并且在命名方法时遵循最少惊喜的原则使用snakecase(register_candidate)而不是camelCase(registerCandidate)。

<强>加

您应该使用strong parameters并将模型传递给哈希值,而不是将params中的每个值复制到模型中。

这可以减少控制器操作之间的代码重复,并保留控制器nice and skinny

class Biodatum < ActiveRecord::Base
  alias_attribute :date_of_birth, :birthday # allows you to take the :birthday param
end

# in your controller:
def register_candidate
   # ...
   @biodata = Biodatum.new(biodatum_params)
   # ...
end

private 

def biodatum_params
   params.require(:biodatum).allow(
    :birthday, :height, :family_members :family_members
   ) # @todo whitelist the rest of the parameters. 
end