Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove rescue that is oculting exceptions on integer columns #7509

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,4 +1,8 @@
## Rails 3.2.9 (unreleased)
* Fix ConnectionAdapters::Column.type_cast_code integer conversion,
to always convert values to integer calling #to_i. Fixes #7509.

*Thiago Pradi*

* Fix `reset_counters` when there are multiple `belongs_to` association with the
same foreign key and one of them have a counter cache.
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/connection_adapters/column.rb
Expand Up @@ -75,7 +75,7 @@ def type_cast(value)

case type
when :string, :text then value
when :integer then value.to_i rescue value ? 1 : 0
when :integer then value.to_i
when :float then value.to_f
when :decimal then klass.value_to_decimal(value)
when :datetime, :timestamp then klass.string_to_time(value)
Expand All @@ -92,7 +92,7 @@ def type_cast_code(var_name)

case type
when :string, :text then var_name
when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)"
when :integer then "(#{var_name}.to_i)"
when :float then "#{var_name}.to_f"
when :decimal then "#{klass}.value_to_decimal(#{var_name})"
when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})"
Expand Down
Expand Up @@ -63,6 +63,12 @@ def test_natural_assignment
assert_equal apple.id, citibank.firm_id
end

def test_id_assignment
apple = Firm.create("name" => "Apple")
citibank = Account.create("credit_limit" => 10)
assert_raise(NoMethodError) { citibank.firm_id = apple }
end

def test_natural_assignment_with_primary_key
apple = Firm.create("name" => "Apple")
citibank = Client.create("name" => "Primary key client")
Expand Down
24 changes: 24 additions & 0 deletions activerecord/test/cases/column_test.rb
Expand Up @@ -24,6 +24,30 @@ def test_type_cast_boolean
assert !column.type_cast('off')
assert !column.type_cast('OFF')
end

def test_type_cast_integer
column = Column.new("field", nil, "integer")
assert_equal 1, column.type_cast(1)
assert_equal 1, column.type_cast('1')
assert_equal 1, column.type_cast('1ignore')
assert_equal 0, column.type_cast('bad1')
assert_equal 0, column.type_cast('bad')
assert_equal 1, column.type_cast(1.7)
assert_nil column.type_cast(nil)
end

def test_type_cast_non_integer_to_integer
column = Column.new("field", nil, "integer")
assert_raises(NoMethodError) do
column.type_cast([])
end
assert_raises(NoMethodError) do
column.type_cast(true)
end
assert_raises(NoMethodError) do
column.type_cast(false)
end
end
end
end
end