Enrico Teotti
2005-Apr-21 15:36 UTC
undefined method `length'' but I use it in the breakpoints
Hi,
I would like to tell rails to generate a errors.add if the (varchar)
parameter I''m inserting is longer then its definition on the DB.
I''ve write in the validate method of the Model, an error unless the
value before type cast is shorter then the column varchar''s limit....
I''ve found that "limit" in Client.columns[n].limit
So:
def validate
#breakpoint
errors.add("ragionesociale", "Troppo
lunga!") unless
ragionesociale_before_type_cast.length <= Client.columns[14].limit
end
should check that ragionesociale''s lenght is not longer than its column
limit.
Using the breakpoint I get the values...
irb(#<Client:0x409c99b4>):003:0> @attributes
=> {"provincia"=>"132",
"ragionesociale"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"contocorrente"=>"132",
"localita"=>"123", "cab"=>"132",
"banca"=>"132",
"indirizzo"=>"123", "fax"=>"132",
"cap"=>"132",
"piva"=>"123", "abi"=>"123",
"sportello"=>nil, "telefono"=>"132",
"email"=>"132"}
irb(#<Client:0x409c99b4>):004:0> ragionesociale_before_type_cast
=> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
irb(#<Client:0x409c99b4>):005:0> ragionesociale_before_type_cast.length
=> 103
but executing normally I get an error:
NoMethodError in Client#crea
undefined method `length'' for nil:NilClass
How could this happen? On the breakpoint I''ve looked at that value...
Thanks for the help,
Enrico
--
"The only thing necessary for the triumph of evil
is for good men to do nothing"
Edmund Burke
Enrico Teotti
2005-Apr-24 10:13 UTC
Re: undefined method `length'' but I use it in the breakpoints
I''ve solved using validates_length_of instead of my way :) 2005/4/21, Enrico Teotti <agenteo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Hi, > I would like to tell rails to generate a errors.add if the (varchar) > parameter I''m inserting is longer then its definition on the DB. > I''ve write in the validate method of the Model, an error unless the > value before type cast is shorter then the column varchar''s limit.... > I''ve found that "limit" in Client.columns[n].limit > > So: > > def validate > #breakpoint > errors.add("ragionesociale", "Troppo lunga!") unless > ragionesociale_before_type_cast.length <= Client.columns[14].limit > end > > should check that ragionesociale''s lenght is not longer than its column limit. > Using the breakpoint I get the values... > > irb(#<Client:0x409c99b4>):003:0> @attributes > => {"provincia"=>"132", > "ragionesociale"=>"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", > "contocorrente"=>"132", "localita"=>"123", "cab"=>"132", > "banca"=>"132", "indirizzo"=>"123", "fax"=>"132", "cap"=>"132", > "piva"=>"123", "abi"=>"123", "sportello"=>nil, "telefono"=>"132", > "email"=>"132"} > irb(#<Client:0x409c99b4>):004:0> ragionesociale_before_type_cast > => "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" > irb(#<Client:0x409c99b4>):005:0> ragionesociale_before_type_cast.length > => 103 > > but executing normally I get an error: > NoMethodError in Client#crea > > undefined method `length'' for nil:NilClass > > How could this happen? On the breakpoint I''ve looked at that value... > Thanks for the help, > Enrico > > -- > "The only thing necessary for the triumph of evil > is for good men to do nothing" > Edmund Burke >-- "The only thing necessary for the triumph of evil is for good men to do nothing" Edmund Burke