# Update multiple rows

**URL:** <https://www.ruby-forum.com/t/update-multiple-rows/138792>\
**Category:** Rails\
**Created:** [May 27, 2008, 8:59pm UTC](https://www.ruby-forum.com/t/update-multiple-rows/138792 "2008-05-27T20:59:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![devxpert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@devxpert](https://www.ruby-forum.com/u/devxpert)\
**Post date:** [May 27, 2008, 8:59pm UTC](https://www.ruby-forum.com/t/update-multiple-rows/138792/1 "2008-05-27T20:59:54Z")

</div>

my query look like this;

“update table\_a set field\_1=‘ABCD’ where field\_2=‘A’;”

it doesn’t work with this:

@arr = table\_a.find(["field\_1 = ? ", “ABCD”])

for item in arr  
item.field\_2 = “A”  
item.save  
end

---

<div class="post-metadata">

**Author:** ![devxpert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@devxpert](https://www.ruby-forum.com/u/devxpert)\
**Post date:** [May 27, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/update-multiple-rows/138792/2 "2008-05-27T21:04:49Z")

</div>

@arr = table\_a.find(:all, :conditions =\> ‘field\_1 = ?’, "ABCD’])

then run your loop… although it should be

for item in @arr ( not just arr, but I assumed that was a typo )

I was guessing at what you wanted, your question isn’t too clear.

Cheers  
sw0rdfish

On May 27, 2:59 pm, Dev X. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![devxpert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@devxpert](https://www.ruby-forum.com/u/devxpert)\
**Post date:** [May 27, 2008, 9:08pm UTC](https://www.ruby-forum.com/t/update-multiple-rows/138792/3 "2008-05-27T21:08:31Z")

</div>

actually I want to update multiple rows with value “A”

sw0rdfish wrote:

> @arr = table\_a.find(:all, :conditions =\> ‘field\_1 = ?’, "ABCD’])
> 
> then run your loop… although it should be
> 
> for item in @arr ( not just arr, but I assumed that was a typo )
> 
> I was guessing at what you wanted, your question isn’t too clear.
> 
> Cheers  
> sw0rdfish
> 
> On May 27, 2:59ï¿½pm, Dev X. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![devxpert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@devxpert](https://www.ruby-forum.com/u/devxpert)\
**Post date:** [May 27, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/update-multiple-rows/138792/4 "2008-05-27T21:11:59Z")

</div>

then my example should work no? IF not what errors do you get?

On May 27, 3:08Â pm, Dev X. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![devxpert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@devxpert](https://www.ruby-forum.com/u/devxpert)\
**Post date:** [May 27, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/update-multiple-rows/138792/5 "2008-05-27T23:03:31Z")

</div>

Dev X. wrote:

> my query look like this;
> 
> “update table\_a set field\_1=‘ABCD’ where field\_2=‘A’;”
> 
> it doesn’t work with this:
> 
> @arr = table\_a.find(["field\_1 = ? ", “ABCD”])

That’s got it all bass-ackwards, you want to find based on field\_2 -  
your where clause, and update field\_1 - the SET statement.

> for item in arr  
> item.field\_2 = “A”  
> item.save  
> end

# brute force, being obvious about what we do

@table\_as = Table\_a.find(:all, :conditions =\> [“field\_2 = ?”, ‘A’])  
for table\_a in @table\_as  
@table\_a = Table\_a.find(table\_a)  
@table\_a.field\_1 = ‘ABCD’  
@table\_a.save  
end

I’m sure there’s a simpler way statement-wise to do it, but that’s off  
the cuff.

---

<div class="post-metadata">

**Author:** ![devxpert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@devxpert](https://www.ruby-forum.com/u/devxpert)\
**Post date:** [May 28, 2008, 12:37am UTC](https://www.ruby-forum.com/t/update-multiple-rows/138792/6 "2008-05-28T00:37:44Z")

</div>

Yes… there is a much simpler way…

Model.update\_all accepts conditions.

- [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001383)

Good luck,

Robby

On Tue, May 27, 2008 at 2:03 PM, Ar Chron  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @table\_as = Table\_a.find(:all, :conditions =\> [“field\_2 = ?”, ‘A’])
> 
> >

–  
Robby R.  
Founder and Executive Director

PLANET ARGON, LLC  
Design, Development, and Hosting with Ruby on Rails

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]
