Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class InflowCategory < ActiveRecord::Base
- end
- class OutflowCategory < ActiveRecord::Base
- end
- class CreateMovementCategories < ActiveRecord::Migration
- def change
- create_table :movement_categories do |t|
- t.string :name
- t.references :user, index: true, foreign_key: true
- t.timestamps null: false
- end
- add_reference :inflows, :movement_category, index: true, foreign_key: true
- add_reference :outflows, :movement_category, index: true, foreign_key: true
- InflowCategory.find_each do |category|
- inflows = Inflow.where(inflow_category_id: category.id)
- new_category = if MovementCategory.where(name: category.name).any?
- MovementCategory.where(name: category.name).first
- else
- MovementCategory.create!(
- name: category.name,
- user_id: category.user_id,
- created_at: category.created_at,
- updated_at: category.updated_at
- )
- end
- inflows.update_all(movement_category_id: new_category.id)
- end
- OutflowCategory.find_each do |category|
- outflows = Outflow.where(outflow_category_id: category.id)
- new_category = if MovementCategory.where(name: category.name).any?
- MovementCategory.where(name: category.name).first
- else
- MovementCategory.create!(
- name: category.name,
- user_id: category.user_id,
- created_at: category.created_at,
- updated_at: category.updated_at
- )
- end
- outflows.update_all(movement_category_id: new_category.id)
- end
- remove_column :inflows, :inflow_category_id
- remove_column :outflows, :outflow_category_id
- drop_table :inflow_categories
- drop_table :outflow_categories
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement