gpt4 book ai didi

ruby-on-rails - 多个 model.save's in 1 if condition with an unless

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:22 26 4
gpt4 key购买 nike

我正在尝试保存一个response 并保存一个issue 如果它在一种情况下不是nil 所以我没有多个 if/else 条件使此逻辑复杂化。

对于 @response 存在且 issue 为 nil 的用例,这不会进入 if block 。

是否有明显的东西我没有看到,或者我不能像这样在一行中写下我的逻辑?

注意:我知道应该使用事务,但我现在只是想建立一个工作原型(prototype)。

  if @response.save && (issue.save unless issue.nil?)  # Does not get into the if block when @response exists and issue is nil
p 'in save'
format.html { redirect_to issue_path(params[:issue_id]), notice: success_message }
else
p 'not in save'
format.html { render action: 'new' }
end

这就是我现在的工作,我希望有一个比这个更简单的 1 类轮。

success = false

if issue.nil?
if @response.save
success = true
end
else
if @response.save && issue.save
success = true
end
end

if success
p 'in save'
format.html { redirect_to issue_path(params[:issue_id]), notice: success_message }
else
p 'not in save'
format.html { render action: 'new' }
end

最佳答案

您想在以下情况下执行“成功”条件:

  1. @response.save 成功并且
  2. issuenilissue 不为 nil 且已成功保存。

这样,你就可以了;

if @response.save and (issue.nil? or issue.save)
# Success
else
# Fail
end

关于ruby-on-rails - 多个 model.save's in 1 if condition with an unless,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23697502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com