Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 具有必须设置为0而不是false的布尔值_Ruby On Rails_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 具有必须设置为0而不是false的布尔值

Ruby on rails 具有必须设置为0而不是false的布尔值,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,我有一个Item对象,它有一个名为has_instore_image的布尔值,当在before_save方法中设置该值时,必须将其设置为0而不是false。这有什么原因吗?这是预期的行为吗?我使用的是Rails 3.2.19 代码: class项0 self.has\u instore\u image=true 其他的 self.has\u instore\u image=0 #self.has\u instore\u image=false 结束 结束 来自Rails文档中的回调: If a

我有一个Item对象,它有一个名为has_instore_image的布尔值,当在before_save方法中设置该值时,必须将其设置为0而不是false。这有什么原因吗?这是预期的行为吗?我使用的是Rails 3.2.19

代码:

class项0
self.has\u instore\u image=true
其他的
self.has\u instore\u image=0
#self.has\u instore\u image=false
结束
结束

来自Rails文档中的回调:

If a before_* callback returns false, all the later callbacks and the associated action are cancelled.
因此,当回调的最后一个表达式的计算结果为
false
,那么回调链将停止,
save
操作将失败

你可以这样补救:

def set_has_instore_image
  if self.instore_images.count>0
    self.has_instore_image=true
  else      
    self.has_instore_image=false
  end
  true
end
事实上,在返回
true
之前结束所有
回调定义被认为是一种良好的做法,以避免同样的问题

def set_has_instore_image
  if self.instore_images.count>0
    self.has_instore_image=true
  else      
    self.has_instore_image=false
  end
  true
end