# DB data type enforcement in Active Record

**URL:** <https://www.ruby-forum.com/t/db-data-type-enforcement-in-active-record/76284>\
**Category:** Rails\
**Created:** [March 3, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/db-data-type-enforcement-in-active-record/76284 "2006-03-03T17:14:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [March 3, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/db-data-type-enforcement-in-active-record/76284/1 "2006-03-03T17:14:21Z")

</div>

I have a question about how ActiveRecord handles data types.

When I enter text in a text\_field (meaning, a field in the GUI) which  
belongs to a numeric field in the database, Active Record automaticly  
converts it to 0, because that’s what the to\_f/to\_i method of a string  
does. Is it also possible to have Active Record enforce the types, so  
that when you enter text for a numeric field, it generates an exception?  
A solution which doesn’t involve specifing a method in each model to  
manually check for types would be nice…

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [March 3, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/db-data-type-enforcement-in-active-record/76284/2 "2006-03-03T17:31:12Z")

</div>

On 03/03/06 17:14 +0100, Wiebe C. wrote:

> I have a question about how ActiveRecord handles data types.
> 
> When I enter text in a text\_field (meaning, a field in the GUI) which  
> belongs to a numeric field in the database, Active Record automaticly  
> converts it to 0, because that’s what the to\_f/to\_i method of a string  
> does. Is it also possible to have Active Record enforce the types, so  
> that when you enter text for a numeric field, it generates an exception?  
> A solution which doesn’t involve specifing a method in each model to  
> manually check for types would be nice…

What you’re looking for is present with Validations. See  
validates\_numericality\_of.

Regards,  
Jason

---

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [March 3, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/db-data-type-enforcement-in-active-record/76284/3 "2006-03-03T17:57:30Z")

</div>

Jason S. wrote:

> What you’re looking for is present with Validations. See  
> validates\_numericality\_of.

Haha, of course. I wanted to use this for my unit tests as well,  
therefore was looking for a way to get an exception. But I can test  
“assert ! object.save”, since it isn’t saved when it doesn’t match the  
validation.

On a sidenote, is there a good way to let AJAX forms pick up on  
validation? Currently, it just does nothing, it fails silently.

---

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [March 5, 2006, 11:44am UTC](https://www.ruby-forum.com/t/db-data-type-enforcement-in-active-record/76284/4 "2006-03-05T11:44:41Z")

</div>

Wiebe C. wrote:

> But I can test  
> “assert ! object.save”, since it isn’t saved when it doesn’t match the  
> validation.  
> Or just “assert object.valid?”…

---

<div class="post-metadata">

**Author:** ![halfgaar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ea666f/32.png) [@halfgaar](https://www.ruby-forum.com/u/halfgaar)\
**Post date:** [March 5, 2006, 12:21pm UTC](https://www.ruby-forum.com/t/db-data-type-enforcement-in-active-record/76284/5 "2006-03-05T12:21:23Z")

</div>

Alex Y. wrote:

> Or just “assert object.valid?”…

Indeed, that’s cleaner.
