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

Ticket #7154 (new defect)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Check type in Array.to_formatted_s(:db)

Reported by: jeremymcanally Assigned to: core
Priority: normal Milestone: 1.2.7
Component: ActiveRecord Version: edge
Severity: normal Keywords: risky
Cc:

Description

to_formatted_s will convert an array of objects to a form usable by a database; I'm guessing from the code that this supposed to be used to put a number of ActiveRecord objects into a sequence for insertion.

This patch checks the type of these objects, because if they are not ActiveRecord objects, Ruby warns that id is deprecated and that you should be using object_id. The patch's behavior makes it where if it is an ActiveRecord object, then it inserts it as the id attribute; otherwise, the object is inserted as its string representation.

Attachments

check_type_in_array_conversion.diff (0.6 kB) - added by jeremymcanally on 01/18/07 03:05:30.

Change History

01/18/07 03:05:30 changed by jeremymcanally

  • attachment check_type_in_array_conversion.diff added.

(follow-up: ↓ 2 ) 01/18/07 22:33:58 changed by dcmanges

If you look at ActiveRecord::Base#quote_bound_value it does this with arrays: value.map { |v| connection.quote(v) }.join(',')

Where is this method used? Is it intended to be used for arrays containing anything besides active records?

(in reply to: ↑ 1 ) 01/18/07 23:11:10 changed by jeremymcanally

Replying to dcmanges:

If you look at ActiveRecord::Base#quote_bound_value it does this with arrays: value.map { |v| connection.quote(v) }.join(',') Where is this method used? Is it intended to be used for arrays containing anything besides active records?

I really don't know; I searched for it, but it's difficult to find because it's a common name for a function. I was going through and documenting a few things and stumbled upon it. When I ran some tests I wrote, it complained about deprecation, so I fixed it. I imagine it might be used when objects are directly inserted to a DB.

04/21/07 00:30:49 changed by josh

  • keywords set to risky.

ArrayExtToSTests.test_to_s_db (activesupport/test/core_ext/array_ext_test.rb) fails because the mock uses a plain old Class instead of AR:Base. So who knows if there are any existing implementations that can be broken because they are not AR classes.