将布尔值传递给 update_attributes 时出现问题
我有以下模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过使用
validates :orderable, :presence => 您的模型实际上表现得完全按照您的指示。 true
验证布尔标志是否存在没有什么意义 - 它将是
true
、nil
或false
- 并且在 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
orfalse
- 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 returnfalse
whenblank?
is called. And, in Rails (with ActiveSupport),false.blank?
evaluates astrue
- which means that your field is failing the validation.Simply remove that validation and everything will work as expected.
就像 Dan Cheail 在他的回答中已经说过的那样,
nil
和false
布尔值在语义上是相同的。但是,如果您确实需要验证它(不允许
nil
),您可以随时执行以下操作:validates_inclusion_of :orderable, :in => [真,假]
Like Dan Cheail already said in his answer, a
nil
andfalse
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]
而不是
validates :presence =>; :true
,您应该使用如下所示的默认值编写迁移:我假设您的默认值应该是
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:I assume your default value should be
false
. Iftrue
, 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 andfalse.blank?
istrue