Greg Hauptmann
2008-Nov-26 21:43 UTC
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")
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-27 07:31 UTC
Re: 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")
On Nov 26, 9:43 pm, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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: http://github.com/rails/rails/tree/2-2-stable/activerecord/lib/active_record/attribute_methods.rb Which is defined here: http://github.com/rails/rails/tree/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> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2008-Nov-27 07:54 UTC
Re: 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")
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 Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Nov 26, 9:43 pm, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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: > http://github.com/rails/rails/tree/2-2-stable/activerecord/lib/active_record/attribute_methods.rb > Which is defined here: > http://github.com/rails/rails/tree/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 >> 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-27 10:15 UTC
Re: 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")
On Nov 27, 7:54 am, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> > On Thu, Nov 27, 2008 at 5:31 PM, Frederick Cheung > > <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Nov 26, 9:43 pm, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > 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: > >http://github.com/rails/rails/tree/2-2-stable/activerecord/lib/active... > > Which is defined here: > >http://github.com/rails/rails/tree/2-2-stable/activerecord/lib/active... > > > (but possibly overriden by individual connection adapters). > > > How that interacts with BaseWithoutTable I have no idea. > > > Fred > >> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2008-Nov-27 10:43 UTC
Re: 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")
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 Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Nov 27, 7:54 am, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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 >> >> On Thu, Nov 27, 2008 at 5:31 PM, Frederick Cheung >> >> <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> > On Nov 26, 9:43 pm, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> > 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: >> >http://github.com/rails/rails/tree/2-2-stable/activerecord/lib/active... >> > Which is defined here: >> >http://github.com/rails/rails/tree/2-2-stable/activerecord/lib/active... >> >> > (but possibly overriden by individual connection adapters). >> >> > How that interacts with BaseWithoutTable I have no idea. >> >> > Fred >> >> 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---