# Can you update a db record without first retrieving it?

**URL:** <https://www.ruby-forum.com/t/can-you-update-a-db-record-without-first-retrieving-it/162537>\
**Category:** Rails\
**Created:** [March 11, 2009, 4:14am UTC](https://www.ruby-forum.com/t/can-you-update-a-db-record-without-first-retrieving-it/162537 "2009-03-11T04:14:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sktib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sktib](https://www.ruby-forum.com/u/sktib)\
**Post date:** [March 11, 2009, 4:14am UTC](https://www.ruby-forum.com/t/can-you-update-a-db-record-without-first-retrieving-it/162537/1 "2009-03-11T04:14:36Z")

</div>

Folks,

I display a set of records to the user who can make changes to one or  
more of those records. When the user saves the changes, I get them back  
in an array that contains only the changed records - with both the  
modified an unmodified attributes for each record. This array also has  
the unique id for each record.

What is the best/fastest way for me to update the records in the db? Or  
do I just have to iterate through the array, retrieving each record  
again from the db and setting up the call to update\_attributes by  
retrieving each attribute individually from the array?

It feels so unlike rails to have to do that and sounds like there must  
be a more efficient way to get all the changed data from the array to  
the database perhaps in a single call.

Appreciate any pointers or help. I am using Rails 2.2 if that is a  
factor in your answer.

Thanks,  
-S

---

<div class="post-metadata">

**Author:** ![sktib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@sktib](https://www.ruby-forum.com/u/sktib)\
**Post date:** [March 11, 2009, 10:59am UTC](https://www.ruby-forum.com/t/can-you-update-a-db-record-without-first-retrieving-it/162537/2 "2009-03-11T10:59:05Z")

</div>

On Mar 11, 3:14 am, Sj Tib [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the best/fastest way for me to update the records in the db? Or  
> do I just have to iterate through the array, retrieving each record  
> again from the db and setting up the call to update\_attributes by  
> retrieving each attribute individually from the array?

Have a look at the update\_all method in the api docs.

Fred
