lundi 25 mai 2015

Update a table in rails

I have a table A(:name, :address, :phone) and want to update the names and addresses of all the records. The following lines of code helps me doing it but takes a lot of time :

A.find_each(batch_size: 10000) do |a|
  new_name = "xyz"
  new_address = "abc"
  A.find(a.id).update(name: name, address: new_address)
end

It takes times because of the last line as I am finding the record first, then updating it. Now when I do this :

A.find_each(batch_size: 10000) do |a|
  a.name = "xyz"
  a.address = "abc"
  a.save
end

The names and addresses don't get updated in this case but a.save returns true. What could be wrong?

Aucun commentaire:

Enregistrer un commentaire