Ruby TinyInt(1)?

时间:2013-04-05 10:34:41

标签: mysql ruby

我有一个用状态标志定义的mysql表(这是一个tinyint(1))。 但是,当我尝试检查值是真还是假时,我似乎得出了错误的结果。也就是说,它没有将价值视为合法的真实或价值,而是测试它是否为“无”或类似 - 因此我的“似乎不起作用”

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))的CORRECT方法是什么,因为它应该是Ruby中的TrueClass或FalseClass;但是(c)本身并不起作用。

这是我使用的参考 - 我假设这应该适用于rails和ruby本身(除非ActiveRecord完成工作) - http://www.orthogonalthought.com/blog/index.php/2007/06/mysql-and-ruby-on-rails-datatypes/

1 个答案:

答案 0 :(得分:2)

来自active_record-3.2.13abstract_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 = false