mserms-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-01 11:58 UTC
AR converts params to integer
Hello. It seems like AR is auto-converting the string from form into an integer (for an attribute of an integer column). However, for custom validation I don''t want it to do this - I want to have access to the actual string. I create the model like this: @model = Model.new(params[:model]) Yet, in Model, logger.info column_name shows 5 (params[:model][:column_name] = 5.6). Any clues on how to change this? Thanks a lot, Mark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 1 November 2006 14:58, mserms-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> It seems like AR is auto-converting the string from form into an > integer (for an attribute of an integer column). However, for custom > validation I don''t want it to do this - I want to have access to the > actual string. > > I create the model like this: > > @model = Model.new(params[:model]) > > Yet, in Model, logger.info column_name shows 5 > (params[:model][:column_name] = 5.6). > > Any clues on how to change this?You should use column_name_before_type_cast reader to get raw data that user has supplied. It is described in AR docs: = Accessing attributes before they have been typecasted ... This is especially useful in validation situations where the user might supply a string for an integer field and you want to display the original string back in an error message. Accessing the attribute normally would typecast the string to 0, which isn’t what you want. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
mserms-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Nov-03 10:13 UTC
Re: AR converts params to integer
Thanks a lot Maxim, that''s exactly what I''d missed. Mark On Nov 2, 8:24 am, Maxim Kulkin <maxim.kul...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1 November 2006 14:58, mse...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> It seems like AR is auto-converting the string from form into an > > integer (for an attribute of an integer column). However, for custom > > validation I don''t want it to do this - I want to have access to the > > actual string. > > > I create the model like this: > > > @model = Model.new(params[:model]) > > > Yet, in Model, logger.info column_name shows 5 > > (params[:model][:column_name] = 5.6). > > > Any clues on how to change this?You should use column_name_before_type_cast reader to get raw data that user > has supplied. It is described in AR docs: > > = Accessing attributes before they have been typecasted > ... > This is especially useful in validation situations where the user might supply > a string for an integer field and you want to display the original string > back in an error message. Accessing the attribute normally would typecast the > string to 0, which isn''t what you want.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---