2

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

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-和-ruby-on-rails-datatypes/

4

1 回答 1

3

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
于 2013-04-05T10:57:02.487 回答