Reputation: 915
I have a table users
that contains details of each user. I'd like to create a new table containing only certain rows from the users
table based on a condition (their age for example). How do I do this in rails ?
Upvotes: 1
Views: 165
Reputation: 1356
You have to create a migration. In this migration you first have to create the new table and then you can choose between two options.
You use pure SQL:
class XMigration < ActiveRecord::Migration
def up
execute <<-SQL
CREATE TABLE new_table
(id INTEGER AUTO_INCREMENT PRIMARY KEY, ...);
INSERT INTO new_table (...)
SELECT ... FROM old_table WHERE condition;
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
Upvotes: 1