Skip to content

Instantly share code, notes, and snippets.

@kazu69
Created October 22, 2015 08:17
Show Gist options
  • Save kazu69/0bcce86aacacd8fbfca0 to your computer and use it in GitHub Desktop.
Save kazu69/0bcce86aacacd8fbfca0 to your computer and use it in GitHub Desktop.
Rails tinyint(1) use boolean false

emulate_booleans

デフォルトではtrue。つまりtinyint(1) はbooleanとして扱うことになる。 tinyint(1) は0~255の値を扱えるのでこのままでは不都合が生じた。

解決策

  1. この設定をfalseにする
  2. activemodel側で再度キャストする

どちらもドキュメントにあるのね。


  1. アプリケーション全体で設定を無効にする
# config/application.rb
require 'active_record/connection_adapters/mysql2_adapter'
ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans = false

see. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L107-L116

  1. Model単位
class StoreListing < ActiveRecord::Base
  attribute :price_in_cents, Type::Integer.new
end

see. https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/attributes.rb#L50-L52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment