Cannot understand rails activerecord typecast reasons?

The reason that this does not work as you expect is that the underlying ruby implementation of BigDecimal does not error when passed a string Consider the following code 'This is a string', '2is a string', '2.3 is also a string', ' -3.3 is also a string'. Each { |d| puts "#{d} = #{BigDecimal. New(d)}" } This is a string = 0.0 2is a string = 2.0 2.3 is also a string = 2.3 -3.3 is also a string = -3.3 So BigDecimal scans the string and assigns anything at the beginning of the string that could be a decimal to its value If you set your model up like this class Dummy 'This is a string') => #, created_at: nil, updated_at: nil> >> puts d.

The_dummy_number 0.0 => nil >> d. Valid? => false >> d.

Errors => ##"This is a string"} This works because the validates_numericality_of macro uses the raw_value method to get at the value before it was typecast and assigned to the internal decimal value.

The reason that this does not work as you expect is that the underlying ruby implementation of BigDecimal does not error when passed a string. Consider the following code 'This is a string', '2is a string', '2.3 is also a string', ' -3.3 is also a string'. Each { |d| puts "#{d} = #{BigDecimal.

New(d)}" } This is a string = 0.0 2is a string = 2.0 2.3 is also a string = 2.3 -3.3 is also a string = -3.3 So BigDecimal scans the string and assigns anything at the beginning of the string that could be a decimal to its value. If you set your model up like this class Dummy > d=Dummy. New(:the_dummy_number => 'This is a string') => #, created_at: nil, updated_at: nil> >> puts d.

The_dummy_number 0.0 => nil >> d. Valid? => false >> d.

Errors => ##"This is a string"} This works because the validates_numericality_of macro uses the raw_value method to get at the value before it was typecast and assigned to the internal decimal value.

Thank you so much.. – ZX12R Jul 27 '10 at 9:56.

This works because the validates_numericality_of macro uses the raw_value method to get at the value before it was typecast and assigned to the internal decimal value.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions