Ruby on rails Rails赢得';无法识别将变量设置为';假';?
我有以下(简化的)模型和迁移: 型号:Ruby on rails Rails赢得';无法识别将变量设置为';假';?,ruby-on-rails,ruby-on-rails-3,rails-migrations,Ruby On Rails,Ruby On Rails 3,Rails Migrations,我有以下(简化的)模型和迁移: 型号: class User < ActiveRecord::Base attr_readonly :contacted validates :contacted, :inclusion => { :in => [true, false] } def set_contacted self.contacted = true end def unset_contacted # self.contacted =
class User < ActiveRecord::Base
attr_readonly :contacted
validates :contacted, :inclusion => { :in => [true, false] }
def set_contacted
self.contacted = true
end
def unset_contacted
# self.contacted = false
self.contacted = "0"
end
end
即使我从模型中删除了“validates”语句,即使我从迁移中删除了NULL约束,我也会得到这个错误。这与将属性的值设置为false有关。ActiveRecord布尔值是否有一些奇怪的限制?如果没有具体的错误信息,回答您的问题有点困难 首先,我将
attr\u readonly
更改为attr\u accessible
——这样字段就可以更新了
其次,我要重新编写您的方法:
def unset_contacted
self.contacted = false
self.save! # Saving your methods (the ! is for throwing an exception if it fails).
end
似乎没有人能解决这个问题,但这对我来说已经不是问题了。使用状态机gem更好地服务于我的模型,因此我将此字段全部删除。您使用的是什么数据库系统?另外,请在您的问题中添加错误消息。我正在使用Postgresql。我收到一整串消息,基本上都是说ActiveRecord“save!”失败了。即使你认为这没有帮助,也最好将错误消息粘贴到问题中。
C:/Ruby193/lib/ruby/gems/activerecord-3.2.8/lib/active_record/validations.rb:56:in 'save!' Validation failed: ActiveRecord::RecordInvalid)
def unset_contacted
self.contacted = false
self.save! # Saving your methods (the ! is for throwing an exception if it fails).
end