加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails booleans bug?

发布时间:2020-12-17 02:59:08 所属栏目:百科 来源:网络整理
导读:我有一个相当神秘的问题处理Rails中的布尔值,这里是如何重现: rails new boolean_bug rails generate model用户验证:布尔值 有了这个,你应该有一个带有User模型的空项目. 3内部boolean_bug / app / models / user.rb “ class User ActiveRecord::Base bef
我有一个相当神秘的问题处理Rails中的布尔值,这里是如何重现:

> rails new boolean_bug
> rails generate model用户验证:布尔值

有了这个,你应该有一个带有User模型的空项目.

3内部boolean_bug / app / models / user.rb

class User < ActiveRecord::Base
 before_save :set_false
 attr_accessible :verified                 

 private  
   def set_false  
     self.verified = false
   end
end

4在控制台中知道重现:

$rails -v 
Rails 3.0.1

$rails console --sandbox
ruby-1.9.2-p0 > User.create!
ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved
from /Users/dpalacio/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:56:in `save!'
from /Users/dpalacio/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:49:in `save!'
from /Users/dpalacio/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/attribute_methods/dirty.rb:30:in `save!

5将self.verified = false更改为self.verified = 0

6再次转到控制台

ruby-1.9.2-p0 > User.create!
 => #<User id: 1,verified: false,created_at: "2010-10-31 04:23:13",updated_at: "2010-10-31 04:23:13">

所以重点是使用false保存不起作用,但是真的,1,0工作,这是一个错误吗?或者我做错了什么?

解决方法

抱歉不是一个rails bug,它实际上是因为before_filter从赋值中返回false,因此活动记录不会保存.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读