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

actually I want to update multiple rows with value "A"

sw0rdfish wrote:

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

Dev Xpert 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.

* http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001383

Good luck,

Robby