Boolean type in Rails & Mysql
MySQL BOOLEAN data type, which is the synonym of TINYINT(1), value range [-128..127]
Confused situation
id (integer) | name (string) | status (boolean) |
---|---|---|
1 | Aoi | 1 |
2 | Maria | 10 |
# Rails 4
Idol.find(2).status
=> false
# Rails 5
Idol.find(2).status
=> true
Explanation
Rails 4 boolean.rb
def cast_value(value)
if value == ''
nil
elsif TRUE_VALUES.include?(value)
true
else
# [...]
false
end
end
Rails 5 boolean.rb
def cast_value(value)
if value == ""
nil
else
!FALSE_VALUES.include?(value)
end
end