Updating objects in arrays

18 views
Skip to first unread message

byrnejb

unread,
Nov 12, 2012, 2:42:50 PM11/12/12
to rubyonra...@googlegroups.com
Perhaps this is a Rails issue.  However, I need to first determine whether I simply am missing something about updating members of an array.

I have an array of 10 ActiveRecord objects.  I wish to iterate over the array and update an attribute.  When I do this the underlying table row changes but the object in the array frequently does not. For example:

  count = 0
  current_entries.each do |entry|
    puts( entry.object_id )
    puts( current_entries[count].object_id )
    puts( "***" )
    puts( entry.lock_version )
    entry = set_customs_entry_status_for( entry, status )
    puts( entry.lock_version )
    current_entries[ count ] = entry
    puts( current_entries[ count ] = entry )
  end

gives:
      58380880
      58380880
      ***
      0
      5
      5
      58378260
      61590300
      ***
      0
      5
      5
      58377460
      58392560
      ***
.  .  .

immediately followed by:

  current_entries.each do |entry|
    puts( entry.lock_version )
  end

gives:
      0
      5
      5
      5
      0
      0
      0
      0
      0
      0
      0
      0
      0

Does anyone see what it is that I am doing wrong?  Why are some, but not all, of the objects in the array unchanged?

Colin Law

unread,
Nov 12, 2012, 3:44:43 PM11/12/12
to rubyonra...@googlegroups.com
On 12 November 2012 19:42, byrnejb <byr...@harte-lyne.ca> wrote:
> Perhaps this is a Rails issue. However, I need to first determine whether I
> simply am missing something about updating members of an array.
>
> I have an array of 10 ActiveRecord objects. I wish to iterate over the
> array and update an attribute. When I do this the underlying table row
> changes but the object in the array frequently does not. For example:
>
> count = 0
> current_entries.each do |entry|
> puts( entry.object_id )
> puts( current_entries[count].object_id )
> puts( "***" )
> puts( entry.lock_version )
> entry = set_customs_entry_status_for( entry, status )

You have not shown us set_customs_entry_status_for but it might be
informative to try a
puts( entry.object_id )
here.

Colin

James B. Byrne

unread,
Nov 12, 2012, 3:54:24 PM11/12/12
to rubyonra...@googlegroups.com

On Mon, November 12, 2012 15:44, Colin Law wrote:
> On 12 November 2012 19:42, byrnejb <byr...@harte-lyne.ca> wrote:
>> Perhaps this is a Rails issue. However, I need to first determine
>> whether I simply am missing something about updating members of
>> an array.
>>
>> I have an array of 10 ActiveRecord objects. I wish to iterate over
>
> You have not shown us set_customs_entry_status_for but it might be
> informative to try a
> puts( entry.object_id ) here.
>

The answer to the source of the problem actually was in the object_ids
given in my original message. I just did not see what my data was
telling me. There is a contingent update on entry when a different
model is updated. This was creating the stale record issue because
the table was being updated via a different set of objects from those
under examination in the test.

--
*** E-Mail is NOT a SECURE channel ***
James B. Byrne mailto:Byr...@Harte-Lyne.ca
Harte & Lyne Limited http://www.harte-lyne.ca
9 Brockley Drive vox: +1 905 561 1241
Hamilton, Ontario fax: +1 905 561 0757
Canada L8E 3C3

Greg Donald

unread,
Nov 12, 2012, 10:56:41 PM11/12/12
to rubyonra...@googlegroups.com
On Mon, Nov 12, 2012 at 1:42 PM, byrnejb <byr...@harte-lyne.ca> wrote:
> I have an array of 10 ActiveRecord objects. I wish to iterate over the
> array and update an attribute. When I do this the underlying table row
> changes but the object in the array frequently does not. For example:
>
> count = 0
> current_entries.each do |entry|
> puts( entry.object_id )
> puts( current_entries[count].object_id )
> puts( "***" )
> puts( entry.lock_version )
> entry = set_customs_entry_status_for( entry, status )

You need to reload the object after updating it if you need to use the
updated version.

set_customs_entry_status_for( entry, status )
entry.reload


--
Greg Donald
Reply all
Reply to author
Forward
0 new messages