mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Cast Common Boolean-Like Values To Booleans as a Rails til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_808 TILs and counting..._
|
_809 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -483,6 +483,7 @@ _808 TILs and counting..._
|
|||||||
- [Attribute Was](rails/attribute-was.md)
|
- [Attribute Was](rails/attribute-was.md)
|
||||||
- [Autosave False On ActiveRecord Associations](rails/autosave-false-on-activerecord-associations.md)
|
- [Autosave False On ActiveRecord Associations](rails/autosave-false-on-activerecord-associations.md)
|
||||||
- [Capybara Page Status Code](rails/capybara-page-status-code.md)
|
- [Capybara Page Status Code](rails/capybara-page-status-code.md)
|
||||||
|
- [Cast Common Boolean-Like Values To Booleans](rails/cast-common-boolean-like-values-to-booleans.md)
|
||||||
- [Change The Nullability Of A Column](rails/change-the-nullability-of-a-column.md)
|
- [Change The Nullability Of A Column](rails/change-the-nullability-of-a-column.md)
|
||||||
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
|
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
|
||||||
- [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md)
|
- [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md)
|
||||||
|
|||||||
28
rails/cast-common-boolean-like-values-to-booleans.md
Normal file
28
rails/cast-common-boolean-like-values-to-booleans.md
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Cast Common Boolean-Like Values To Booleans
|
||||||
|
|
||||||
|
Sometimes you have to deal with values that are supposed to represent booleans,
|
||||||
|
but they aren't actually boolean values (i.e. `"t"` instead of `true`). Rail's
|
||||||
|
`ActiveModel` has a helper for casting these common boolean-like values to
|
||||||
|
actual booleans.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> ActiveModel::Type::Boolean.new.cast('f')
|
||||||
|
=> false
|
||||||
|
> ActiveModel::Type::Boolean.new.cast('t')
|
||||||
|
=> true
|
||||||
|
> ActiveModel::Type::Boolean.new.cast('true')
|
||||||
|
=> true
|
||||||
|
> ActiveModel::Type::Boolean.new.cast('FALSE')
|
||||||
|
=> false
|
||||||
|
> ActiveModel::Type::Boolean.new.cast(0)
|
||||||
|
=> false
|
||||||
|
> ActiveModel::Type::Boolean.new.cast(1)
|
||||||
|
=> true
|
||||||
|
> ActiveModel::Type::Boolean.new.cast(true)
|
||||||
|
=> true
|
||||||
|
> ActiveModel::Type::Boolean.new.cast(false)
|
||||||
|
=> false
|
||||||
|
```
|
||||||
|
|
||||||
|
This cast method gives you a handy way to handle all thsoe different cases.
|
||||||
|
This is available as of Rails 5+.
|
||||||
Reference in New Issue
Block a user