Ruby on rails 未在服务对象内引发ActiveRecord异常

Ruby on rails 未在服务对象内引发ActiveRecord异常,ruby-on-rails,ruby,Ruby On Rails,Ruby,在构建服务对象时,RecordInvalid异常不会在应该抛出时抛出。而是每次运行else块中的代码 # services/new_registration_service.rb class NewRegistrationService ... def perform begin account_create rescue ActiveRecord::RecordInvalid => exception OpenStruct.

在构建服务对象时,
RecordInvalid
异常不会在应该抛出时抛出。而是每次运行
else
块中的代码

# services/new_registration_service.rb
class NewRegistrationService

  ...  

  def perform    
    begin
      account_create
    rescue ActiveRecord::RecordInvalid => exception
      OpenStruct.new(success?: false, user: user, account: account, error: exception.message)
    else
      # this is running every time
      OpenStruct.new(success?: true, user: user, account: account, error: nil)
    end
  end

  private

    ...

    def account_create
      # this is NOT saving, which I believe should
      # throw the exception in the perform method
      post_account_setup if account.save
    end

    ...

end
这就是我从规范运行的内容,其中需要
account:name

post :create, params: { account: FactoryBot.attributes_for(:account, { name: nil }) }
即使我
放入
返回的
account.name
值,它也是
nil
。。。这就是引发
记录无效的异常的原因

# models/account.rb
class Account < ApplicationRecord
  resourcify
  has_many :users
  validates :name, presence: true
end
#models/account.rb
类帐户<应用程序记录
资源化
有很多:用户
验证:名称,状态:true
终止
有什么想法吗

def account_create
  # Instead of this:
  post_account_setup if account.save

  # Do this:
  account.save!
  post_account_setup
end
调用
save
而不是
save不会引发异常;它只会返回
false

或者,有些人会争辩说,像这样使用异常来控制流是不好的做法。因此,您可以做如下操作:

def perform
  # ...
  if account.valid?
    account.save! # This should never fail!
    post_account_create
    OpenStruct.new(success?: true, user: user, account: account, error: nil)
  else
    OpenStruct.new(success?: false, user: user, account: account, error: account.errors)
  end
end
或者,类似地:

def perform
  # ...
  if account.save
    post_account_create
    OpenStruct.new(success?: true, user: user, account: account, error: nil)
  else
    OpenStruct.new(success?: false, user: user, account: account, error: account.errors)
  end
end
调用
save
而不是
save不会引发异常;它只会返回
false

或者,有些人会争辩说,像这样使用异常来控制流是不好的做法。因此,您可以做如下操作:

def perform
  # ...
  if account.valid?
    account.save! # This should never fail!
    post_account_create
    OpenStruct.new(success?: true, user: user, account: account, error: nil)
  else
    OpenStruct.new(success?: false, user: user, account: account, error: account.errors)
  end
end
或者,类似地:

def perform
  # ...
  if account.save
    post_account_create
    OpenStruct.new(success?: true, user: user, account: account, error: nil)
  else
    OpenStruct.new(success?: false, user: user, account: account, error: account.errors)
  end
end

您可以简单地在一行中处理这个问题(为了便于阅读,这里将其分为两行):

如果
post\u account\u create
可以移动到模型中,我建议是a,即

account.rb

after_create :post_account_create
有关生命周期的更多信息

account.errors
将返回一个空数组(如果没有),根据它的空性或响应的成功性,在其他地方应该不难处理


希望这是有用的。

您可以在一行中简单地处理这个问题(为了便于阅读,这里分成两行):

如果
post\u account\u create
可以移动到模型中,我建议是a,即

account.rb

after_create :post_account_create
有关生命周期的更多信息

account.errors
将返回一个空数组(如果没有),根据它的空性或响应的成功性,在其他地方应该不难处理


希望有用。

您是否尝试过
account.save
而不是
account.save
?您是否尝试过
account.save而不是
帐户。是否保存