I want to copy all the rows from my table with single column update.
Example.
Table County has 1000 rows and i want to copy all the rows with single column update.
What is the proper way to do that ?
I want to copy all the rows from my table with single column update.
Example.
Table County has 1000 rows and i want to copy all the rows with single column update.
What is the proper way to do that ?
Assuming I understand you correctly, I'd do something like
INSERT INTO NewTable (Col1, Col2, Col3)
SELECT Col1, Col2, UpdateFunction(Col3)
FROM County
Where Col3 is the column you want to update, and UpdateFunction is the function you wish to use to update the column.
EDIT: Of course this is SQL, not Rails - I didn't look close enough at the question's tags :-)
This question describes how to use clone
to copy your record.
Assuming the 1000 records are already in an Enumerable called counties
, we end up with
counties.each { |county|
county_copy = county.clone
county_copy.col3 = update_function(county_copy.col3)
county_copy.save
}