首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby TinyInt(1)?

Ruby TinyInt(1)?
EN

Stack Overflow用户
提问于 2013-04-05 18:34:41
回答 1查看 993关注 0票数 2

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

代码语言:javascript
复制
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/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-05 18:57:02

active_record-3.2.13中,abstract_mysql_adapter.rb代码行96:

代码语言:javascript
复制
  # 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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15831692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档