gpt4 book ai didi

mysql - ruby TinyInt(1)?

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:02 25 4
gpt4 key购买 nike

我有一个用状态标志定义的 mysql 表(它是一个 tinyint(1))。但是,当我尝试检查该值是真还是假时,我似乎得出了错误的结果。也就是说,它不会将该值视为合法的 true 或值,而是测试它是否不是“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-and-ruby-on-rails-datatypes/

最佳答案

来自 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

关于mysql - ruby TinyInt(1)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15831692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com