LoginSignup
1
1

More than 5 years have passed since last update.

emulate_booleansをモデルごとに有効にする

Posted at

環境

Rails 5.0.6
ruby 2.5.1

はじめに

railsにはtinyintをbooleanとして認識することができる便利な機能があります。
しかし、昔から運用しているアプリケーションでは、容易にtrueにはできない状況が発生します。

# たとえば以下のコードは、tinyint_columnがbooleanになってしまうと条件がコケる
if tinyint_column == 1
  ...
end

そのため、泣く泣くこのオプションをOFFにしていました :sob:

config/application.rb
ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans = false

しかし、booleanで扱えるといろいろ便利な場面が多いのも事実です。
新規で作成するモデルや、利用するgemのモデルではぜひとも利用したい!! :smile:

ということで、やってみました :muscle:

対処法

今回は、moduleを作り、対象モデルでincludeすることで実現します。

app/lib/emulate_boolean_type.rb
module EmulateBooleanType
  extend ActiveSupport::Concern

  included do
    emulate_boolean_type!
  end

  class_methods do
    def emulate_boolean_type!
      return unless ActiveRecord::Base.connection.data_source_exists? table_name
      tinyint_columns = columns_hash.select{|_name, col| col.sql_type == 'tinyint(1)'}.keys.map(&:to_sym)
      tinyint_columns.each do |col|
        attribute col, ActiveModel::Type::Boolean.new
      end
    end
  end
end
app/models/hoge.rb
class Hoge
  include(EmulateBooleanType)
  ...
end

以上です。
めでたしめでたし :clap:

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1