# \_before\_type\_cast

**URL:** <https://www.ruby-forum.com/t/before-type-cast/51174>\
**Category:** Rails\
**Created:** [November 14, 2005, 7:50am UTC](https://www.ruby-forum.com/t/before-type-cast/51174 "2005-11-14T07:50:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 14, 2005, 7:50am UTC](https://www.ruby-forum.com/t/before-type-cast/51174/1 "2005-11-14T07:50:34Z")

</div>

Hi,

When ActiveRecord gets any field from the database does it receive the  
field  
as a string regardless of the field’s type in the db?

And this is why ActiveRecord must subsequently cast the data into a Ruby  
type?

If the answers are yes, then is it correct to say that the only way  
databases export data for any request is through strings? (If so, wow,  
primative but understandable)

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 14, 2005, 8:59am UTC](https://www.ruby-forum.com/t/before-type-cast/51174/2 "2005-11-14T08:59:45Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 13, 2005, at 10:48 PM, Peter M. wrote:

> When ActiveRecord gets any field from the database does it receive  
> the field as a string regardless of the field’s type in the db?
> 
> And this is why ActiveRecord must subsequently cast the data into a  
> Ruby type?
> 
> If the answers are yes, then is it correct to say that the only way  
> databases export data for any request is through strings? (If so,  
> wow, primative but understandable)

The other way around: all data from the web (query and form  
parameters, cookies, headers, etc) are strings. So  
foo\_before\_type\_cast gives you the “source” string whereas foo is  
type-cast according to the database column type.

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDeEOIAQHALep9HFYRAqbvAKC3ERFDbWljsnMbEys0BcFIXjfauwCfYUzj  
jAZ8PessmkTbMNEtE680oiU=  
=00+R  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 14, 2005, 1:13pm UTC](https://www.ruby-forum.com/t/before-type-cast/51174/3 "2005-11-14T13:13:46Z")

</div>

Now I’m feeling dense. How does the database send the info to rails when  
rails makes a request? INT is sent differently then TEXT or DATE?

Peter

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 14, 2005, 7:33pm UTC](https://www.ruby-forum.com/t/before-type-cast/51174/4 "2005-11-14T19:33:38Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 14, 2005, at 4:12 AM, Peter M. wrote:

> Now I’m feeling dense. How does the database send the info to rails  
> when rails makes a request? INT is sent differently then TEXT or DATE?

All the database drivers do it differently. Most have a binary  
protocol that efficiently encodes each data type. The driver speaks  
that protocol and spits out numbers, strings, etc. This isn’t  
enough, though, since we want the all the drivers to operate  
uniformly, so the connection adapters translate the driver’s results  
into a standard set of Ruby classes according to the database column  
types.

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDeNgAAQHALep9HFYRAsl/AKCYAwr1coU4jIbeNG/6AoudIQYeywCgojDV  
V74JufdvmK3agYjSqaE02Bw=  
=YgDV  
-----END PGP SIGNATURE-----
