Rails 3 database porting
Had to quickly slap something together to move a table (approx 30k records) from one db to another with different names.
My hack solution using RoR was this:
for old_line in SaleItemsOld.where(:sale_id => old_sale.id)
line = LineItems.new
line.sale_id = new_sale.id
...
line.save
end
I had the thought that this should be sped up by constructing all these new lines as SQL queries in a single string and then executing开发者_如何转开发 that in one go.
Is there a better Rails solution for this?
Usually you can do this sort of thing with a migration and some carefully constructed SQL. Generally the idea is do to something along the lines of:
INSERT INTO line_items (sale_id) SELECT id FROM sale_items_old
You can add other values as required both to the insert list and the select specification.
In a migration you can run arbitrary SQL by placing it in your up
method:
def self.up
execute("...")
end
精彩评论