将布尔值传递给 update_attributes 时出现问题

发布于 2024-10-20 09:54:46 字数 618 浏览 2 评论 0原文

我有以下模型:

class GuestCatering < ActiveRecord::Base

  # Validation
  validates :name, :presence => true
  validates :order_number, :presence => true
  validates :orderable, :presence => true

end

但是当我尝试使用以下代码更新现有的 GuestCatering 时:

guest_catering.update_attributes(:orderable => false)

宾客餐饮变量是有效的 GuestCatering 对象。 guest_catering 对象在更新后出现错误,如下所示:

<{[:orderable, ["can't be blank"]]=>nil}>

但是当我传递 orderable =>; true,一切都很好,没有错误。

这里出了什么问题,为什么我不能将 orderable 设置为 false?

I've got the following Model:

class GuestCatering < ActiveRecord::Base

  # Validation
  validates :name, :presence => true
  validates :order_number, :presence => true
  validates :orderable, :presence => true

end

But when I'll try to update an existing GuestCatering with the following code:

guest_catering.update_attributes(:orderable => false)

The guest catering variable is a valid GuestCatering object.
The guest_catering object has errors after the update, like that:

<{[:orderable, ["can't be blank"]]=>nil}>

But when i pass a orderable => true, everything is fine and no errors.

What's wrong here, why can't i set orderable to false?

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

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

发布评论

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

评论(3

樱花落人离去 2024-10-27 09:54:46

通过使用 validates :orderable, :presence => 您的模型实际上表现得完全按照您的指示。 true

验证布尔标志是否存在没有什么意义 - 它将是 truenilfalse - 并且在 Ruby 中world、nil 和 false 在布尔逻辑中具有相同的语义值。

在内部,validates :presence 依赖于被检查的属性的值,以便在调用 blank? 时返回 false。而且,在 Rails(使用 ActiveSupport)中,false.blank? 计算结果为 true - 这意味着您的字段未通过验证。

只需删除该验证,一切都会按预期进行。

Your model is actually behaving exactly as you told it to, through your use of validates :orderable, :presence => true

There's little point validating the presence of a boolean flag - it's going to be true, nil or false - and in Ruby world, nil and false have the same semantic value when it comes to boolean logic.

Internally, validates :presence relies on the value of the attribute being checked to return false when blank? is called. And, in Rails (with ActiveSupport), false.blank? evaluates as true - which means that your field is failing the validation.

Simply remove that validation and everything will work as expected.

歌枕肩 2024-10-27 09:54:46

就像 Dan Cheail 在他的回答中已经说过的那样,nilfalse 布尔值在语义上是相同的。

但是,如果您确实需要验证它(不允许 nil),您可以随时执行以下操作:

validates_inclusion_of :orderable, :in => [真,假]

Like Dan Cheail already said in his answer, a nil and false boolean is semantically the same thing.

But, if you really need to validate it (not allowing nil), you can always do :

validates_inclusion_of :orderable, :in => [true, false]

﹂绝世的画 2024-10-27 09:54:46

而不是 validates :presence =>; :true,您应该使用如下所示的默认值编写迁移:

t.boolean :orderable, :default => 0

我假设您的默认值应该是 false。如果true,则使用 1 作为默认值。然后它将在数据库中设置默认值。因此,您可以省略验证检查。

@dan 回答了您无法使用 validates :presence 的原因。存在意味着不为空,Rails 使用 .blank? 函数来实现此目的,并且 false.blank?true

Instead of validates :presence => :true, you should write your migrations with the default value like this:

t.boolean :orderable, :default => 0

I assume your default value should be false. If true, use 1 as default. Then it will set the default value in database. So, you can omit the validation check.

The reason you cannot use validates :presence is answered by @dan. Presence means not blank and Rails use .blank? function for this and false.blank? is true

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