Rails 模型变量意外变化

发布于 2024-12-11 09:30:10 字数 1328 浏览 0 评论 0原文

在这里要发疯了。如有指点,不胜感激!

我有一个交付模型,我正在尝试添加一种方法来根据交付线路更新交付状态。该函数是在模型类中定义的,delivery_state 是模型属性之一:

def updateDeliveryState
  expectedLines = DeliveryLine.where( :delivery_id => id,
                                      :line_state => 'EXPECTED' )
  logger.debug "State1: #{delivery_state}"

  if expectedLines.length == 0
    if delivery_state == 'EXPECTED' || delivery_state == 'RECEIVING'
      delivery_state = 'RECEIVED'               # commenting this line fixes it
      save
    end
  else
    logger.debug "State2: #{delivery_state}"
    if delivery_state == 'EXPECTED'
      logger.debug "Updating to receiving"
      delivery_state = 'RECEIVING'
      save
    end
  end
end

我在日志中看到的是,在 2 logger.debug 行之间,delivery_state 已被清除:

State1: EXPECTED
DeliveryLine Load (4.5ms)  SELECT "delivery_lines".* FROM "delivery_lines" 
WHERE "delivery_lines"."line_state" = 'EXPECTED' 
AND "delivery_lines"."delivery_id" = 227
State2:

如果我注释掉上面代码中标记的行,它似乎工作正常:

State1: EXPECTED
DeliveryLine Load (9.6ms)  SELECT "delivery_lines".* FROM "delivery_lines" 
WHERE "delivery_lines"."line_state" = 'EXPECTED' 
AND "delivery_lines"."delivery_id" = 227
State2: EXPECTED
Updating to receiving

但是,刷新后我可以看到在此之后交付仍然是预期的?

Going mad here. Any pointers gratefully received!

I've got a Delivery model and I'm trying to add a method to update the delivery state, based on delivery lines. This function is defined within the model class, and delivery_state is one of the model attributes:

def updateDeliveryState
  expectedLines = DeliveryLine.where( :delivery_id => id,
                                      :line_state => 'EXPECTED' )
  logger.debug "State1: #{delivery_state}"

  if expectedLines.length == 0
    if delivery_state == 'EXPECTED' || delivery_state == 'RECEIVING'
      delivery_state = 'RECEIVED'               # commenting this line fixes it
      save
    end
  else
    logger.debug "State2: #{delivery_state}"
    if delivery_state == 'EXPECTED'
      logger.debug "Updating to receiving"
      delivery_state = 'RECEIVING'
      save
    end
  end
end

What I'm seeing in the log is that between the 2 logger.debug lines, the delivery_state has been cleared:

State1: EXPECTED
DeliveryLine Load (4.5ms)  SELECT "delivery_lines".* FROM "delivery_lines" 
WHERE "delivery_lines"."line_state" = 'EXPECTED' 
AND "delivery_lines"."delivery_id" = 227
State2:

If I comment out the line marked in the above code, it appears to work OK:

State1: EXPECTED
DeliveryLine Load (9.6ms)  SELECT "delivery_lines".* FROM "delivery_lines" 
WHERE "delivery_lines"."line_state" = 'EXPECTED' 
AND "delivery_lines"."delivery_id" = 227
State2: EXPECTED
Updating to receiving

However, I can see after refreshing that the delivery is still EXPECTED after this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2024-12-18 09:30:10

详细说明我的评论:看起来您正在 if 中创建一个局部变量。看:

class Foo
  attr_accessor :bar

  def test
    unless bar
      bar = 1
    end
  end
end

f = Foo.new
f.test
puts f.bar # empty line, bar is nil

现在让我们确保调用设置器:

class Foo
  attr_accessor :bar

  def test
    unless bar
      self.bar = 1
    end
  end
end

f = Foo.new
f.test
puts f.bar # prints 1

请参阅:为什么红宝石镶工需要“自我”。班级内的资格?

To elaborate on my comment: it seems like you are creating a local variable in the if. Look:

class Foo
  attr_accessor :bar

  def test
    unless bar
      bar = 1
    end
  end
end

f = Foo.new
f.test
puts f.bar # empty line, bar is nil

Now let's make sure we call the setter:

class Foo
  attr_accessor :bar

  def test
    unless bar
      self.bar = 1
    end
  end
end

f = Foo.new
f.test
puts f.bar # prints 1

See: Why do ruby setters need “self.” qualification within the class?

风情万种。 2024-12-18 09:30:10

您可以尝试使用 save(:validate => false) 吗?

有时 Rails 有这种令人讨厌的习惯,即默默地使验证失败并且不保存。

无论如何,你可能都想保存你的状态......:)

can you try to use save(:validate => false) ?

Sometimes Rails has this nasty habit of silently failing validations and not saving.

You probably want to save your state no matter what... :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文