Update multiple rows

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

@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. [email protected]

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. [email protected]

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

On May 27, 3:08Â pm, Dev X. [email protected]

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.

Yes… there is a much simpler way…

Model.update_all accepts conditions.

Good luck,

Robby

On Tue, May 27, 2008 at 2:03 PM, Ar Chron
[email protected] 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

http://www.robbyonrails.com/
aim: planetargon

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