# Schema Coercion mysql / sqlite integer to boolean

**URL:** <https://discourse.rom-rb.org/t/schema-coercion-mysql-sqlite-integer-to-boolean/551>\
**Category:** Issues\
**Created:** [August 8, 2022, 5:09pm UTC](https://discourse.rom-rb.org/t/schema-coercion-mysql-sqlite-integer-to-boolean/551 "2022-08-08T17:09:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![codeslinger](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@codeslinger](https://discourse.rom-rb.org/u/codeslinger)\
**Post date:** [August 8, 2022, 5:09pm UTC](https://discourse.rom-rb.org/t/schema-coercion-mysql-sqlite-integer-to-boolean/551/1 "2022-08-08T17:09:07Z")

</div>

Can someone give me an example of how this can be done? OR how I should be reading these values (0/1) from the database to have them represented by ruby false and true?

Thanks!

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [August 10, 2022, 7:45am UTC](https://discourse.rom-rb.org/t/schema-coercion-mysql-sqlite-integer-to-boolean/551/2 "2022-08-10T07:45:47Z")

</div>

You can use a custom read type, ie `attribute :admin, Types::Integer, read: Types::Params::Bool`

---

<div class="post-metadata">

**Author:** ![codeslinger](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@codeslinger](https://discourse.rom-rb.org/u/codeslinger)\
**Post date:** [August 10, 2022, 4:14pm UTC](https://discourse.rom-rb.org/t/schema-coercion-mysql-sqlite-integer-to-boolean/551/3 "2022-08-10T16:14:36Z")

</div>

Thanks! This works of course, but it is unclear why it works. The dry types state

> `params` - types with constructors performing non-strict coercions specific to HTTP parameters

Why use the Params type category over others when reading from the db?

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/alassek/32/260_2.png) [@alassek](https://discourse.rom-rb.org/u/alassek)\
**Post date:** [August 10, 2022, 10:48pm UTC](https://discourse.rom-rb.org/t/schema-coercion-mysql-sqlite-integer-to-boolean/551/4 "2022-08-10T22:48:01Z")

</div>

In this case, because `Params` does the loose type coercion that you need for numeric booleans.

By default, dry-types is strict about type checking. Try it and see:

```ruby
Types::Strict::Bool[1]
# => Dry::Types::ConstraintError: 1 violates constraints (type?(FalseClass, 1) failed)

```

An integer is not a boolean in Ruby, and you don’t want that to be the case most of the time. Ruby is a strongly typed language, and so if you intend to convert a number to a boolean it should be done explicitly.

Interacting with a different type system is a special case, and `Params` is there to do those kind of coercions. It’s not specifically meant for databases, but it works here.

In more advanced cases, you might want to define your own coercion rules for custom types. For instance, this boolean coercion might be written as

```ruby
Bool = Types::Strict::Bool.constructor do |input, type|
  case input
  when true, 1, 't', 'TRUE' then true
  when false, 0, 'f', 'FALSE' then false
  else type.(input)
end

```

---

<div class="post-metadata">

**Author:** ![codeslinger](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@codeslinger](https://discourse.rom-rb.org/u/codeslinger)\
**Post date:** [August 11, 2022, 2:41pm UTC](https://discourse.rom-rb.org/t/schema-coercion-mysql-sqlite-integer-to-boolean/551/5 "2022-08-11T14:41:26Z")

</div>

Thank you kindly for your detail response. This is helpful!
