value_to_boolean deprecated; what's a good replacement?

前端 未结 4 1549
醉话见心
醉话见心 2021-02-01 03:59

Is there a \"cool-kid-approved\" replacement for ActiveRecord::ConnectionAdapters::Column.value_to_boolean in rails 3.2?

4条回答
  •  离开以前
    2021-02-01 04:42

    In Rails 4.2, this looks like a possible way to do it:

     ActiveRecord::Type::Boolean.new.type_cast_from_database(value)
    

    Which under the covers is going to do this

    if value == ''
      nil
    else
      ConnectionAdapters::Column::TRUE_VALUES.include?(value)
    end
    

    Or in Rails 5:

     ActiveRecord::Type::Boolean.new.cast(value)
    

    Which seems to end up here:

      def cast_value(value)
        if value == ''
          nil
        else
          !FALSE_VALUES.include?(value)
        end
      end
    

提交回复
热议问题