我有一个定义了状态标志(即tinyint(1))的mysql表。但是,当我尝试检查该值是true还是false时,我似乎得到了错误的结果。也就是说,它不会将值视为合法的true或value,而是如果不是"nil“或类似值,则将其视为test的值-因此我的”似乎不起作用“
results.each_hash do |row|
# What I tried
# (a)
if row['status']
# do something - doesn't seem to work
end
# (b)
if row['status'].to_i == 1
# this seems correct
end
# (c)
if row['status'] == false
# doesn't seem to work
end
end检查这个值(tinyint(1))的正确方法是什么,因为它应该是Ruby语言中的TrueClass或FalseClass;然而(c)本身并不起作用。
这是我使用的一个引用--我假设这应该适用于rails和ruby本身(除非是ActiveRecord完成了这项工作)- http://www.orthogonalthought.com/blog/index.php/2007/06/mysql-and-ruby-on-rails-datatypes/
发布于 2013-04-05 18:57:02
在active_record-3.2.13中,abstract_mysql_adapter.rb代码行96:
# By default, the MysqlAdapter will consider all columns of type <tt>tinyint(1)</tt>
# as boolean. If you wish to disable this emulation (which was the default
# behavior in versions 0.13.1 and earlier) you can add the following line
# to your application.rb file:
#
# ActiveRecord::ConnectionAdapters::Mysql[2]Adapter.emulate_booleans = falsehttps://stackoverflow.com/questions/15831692
复制相似问题