Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11398 (new defect)

Opened 4 months ago

Last modified 3 months ago

[PATCH] Float columns set to values without .to_f cause NoMethodError on save or validation

Reported by: h-lame Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: float column tiny
Cc: h-lame

Description

Imagine the following:

create_table :vehicle do |t|
 t.integer :wheels
 t.float :top_speed
end

v = Vehicle.new
v.wheels = []
v.top_speed = 10.2
v.save #=> true

v.top_speed = []
v.save #=> NoMethodError: undefined method `to_f' for []:Array

No matter how weird this example may seem (why are we setting an empty array as the value of a float column?) there's an issue of consistency in that you can do it for integer based columns but not float based columns. Looking at most of the other type casting methods in schema_definitions.rb it would seem that most other column types would do something other than fail with an Error. Also when we consider AR outside of the web development space and as a standard ORM, code is much more likely to mistakenly set values that are not strings (e.g. cope with .to_f being called on them) so we should cope sensibly and leave it up to validations to argue about the correctness of the data going in.

Whether or not we think that forcing non to_f-able values to a float is a good thing or not is really a separate issue (see #608 for a discussion on that) we should aim at consistency and only then aim at a particular standpoint on the issue (given what AR does now I think we can assume that AR believes forcing values is good). Right now we have no consistency, so we can't even begin to argue about a standpoint.

Attachments

fix_type_casting_so_float_columns_behave_similarly_to_integer_columns.diff (1.3 kB) - added by h-lame on 03/22/08 12:16:48.
Tiny fix to schema_definitions.rb that will make float column type casting behave similarly to integers
tests_to_show_how_integer_and_float_columns_behave_differently.diff (1.6 kB) - added by h-lame on 03/29/08 19:45:26.
Tests that will show how integer columns and float columns behave differently

Change History

03/22/08 12:16:48 changed by h-lame

  • attachment fix_type_casting_so_float_columns_behave_similarly_to_integer_columns.diff added.

Tiny fix to schema_definitions.rb that will make float column type casting behave similarly to integers

03/22/08 12:17:48 changed by h-lame

Oh, um, the test patch also fixes test/case/base_test.rb so it can be run independently of the other tests by requiring some missing models.

03/26/08 14:00:28 changed by thechrisoshow

+1 - works a treat AND tests pass.

03/29/08 19:45:26 changed by h-lame

  • attachment tests_to_show_how_integer_and_float_columns_behave_differently.diff added.

Tests that will show how integer columns and float columns behave differently

03/29/08 19:46:23 changed by h-lame

Reworked test patch to no longer fix test/cases/base_test.rb now that #11425 has been committed.