# When/where does Rails convert "params" used to populate a model object from a form, into their corre

**URL:** <https://www.ruby-forum.com/t/when-where-does-rails-convert-params-used-to-populate-a-model-object-from-a-form-into-their-corre/154258>\
**Category:** Rails\
**Created:** [November 26, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/when-where-does-rails-convert-params-used-to-populate-a-model-object-from-a-form-into-their-corre/154258 "2008-11-26T22:44:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 26, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/when-where-does-rails-convert-params-used-to-populate-a-model-object-from-a-form-into-their-corre/154258/1 "2008-11-26T22:44:18Z")

</div>

Hi,

When/where does Rails convert “params” used to populate a model object  
from a form, into their correct types in the model object??? (i.e.  
from “string”) What part of rails does this?

I have a form upload and in my create method where I go  
:@graph\_options = GraphOptions.new(params[:graph\_options])" it is not  
doing the type conversion, as the attributes remain as strings.

Note I am using BaseWithoutTable

* * *

## class GraphOptions \< ActiveRecord::BaseWithoutTable column :label\_title, :string column :start\_date, :date column :end\_date, :date column :granularity, :integer column :chart\_type, :integer . . .

thanks

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 27, 2008, 8:32am UTC](https://www.ruby-forum.com/t/when-where-does-rails-convert-params-used-to-populate-a-model-object-from-a-form-into-their-corre/154258/2 "2008-11-27T08:32:13Z")

</div>

On Nov 26, 9:43 pm, “Greg H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> When/where does Rails convert “params” used to populate a model object  
> from a form, into their correct types in the model object??? (i.e.  
> from “string”) What part of rails does this?

Well it’s the call to column.typecast here:

> <https://github.com/rails/rails/blob/2-2-stable/activerecord/lib/active_record/attribute_methods.rb>

Which is defined here:

> <https://github.com/rails/rails/blob/2-2-stable/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb>

(but possibly overriden by individual connection adapters).

How that interacts with BaseWithoutTable I have no idea.

Fred

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 27, 2008, 8:55am UTC](https://www.ruby-forum.com/t/when-where-does-rails-convert-params-used-to-populate-a-model-object-from-a-form-into-their-corre/154258/3 "2008-11-27T08:55:03Z")

</div>

thanks - I’ll have a look at the code - so would it be correct to say  
then that the Rails framework does effectively cast the params strings  
to their type prior to any saves to the data? (i.e. its not like it  
leave this up to the database to do itself is it, i.e. like if there  
was a date it doesn’t just pass the string representation of the data  
through to the database to store in it’s Date field)

On Thu, Nov 27, 2008 at 5:31 PM, Frederick C.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 27, 2008, 11:15am UTC](https://www.ruby-forum.com/t/when-where-does-rails-convert-params-used-to-populate-a-model-object-from-a-form-into-their-corre/154258/4 "2008-11-27T11:15:56Z")

</div>

On Nov 27, 7:54 am, “Greg H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> thanks - I’ll have a look at the code - so would it be correct to say  
> then that the Rails framework does effectively cast the params strings  
> to their type prior to any saves to the data? (i.e. its not like it  
> leave this up to the database to do itself is it, i.e. like if there  
> was a date it doesn’t just pass the string representation of the data  
> through to the database to store in it’s Date field)

Actually it doesn’t (minor exception in that a blank string is turned  
into nil for numbers). typecasting happens in read\_attribute (check  
the source for read/write\_attribute.

Fred

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [November 27, 2008, 11:44am UTC](https://www.ruby-forum.com/t/when-where-does-rails-convert-params-used-to-populate-a-model-object-from-a-form-into-their-corre/154258/5 "2008-11-27T11:44:40Z")

</div>

ohhh, thanks. Things make sense now when I look at my object state in  
debug & see the strings…

On Thu, Nov 27, 2008 at 8:15 PM, Frederick C.
