Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. class InflowCategory < ActiveRecord::Base
  2. end
  3.  
  4. class OutflowCategory < ActiveRecord::Base
  5. end
  6.  
  7. class CreateMovementCategories < ActiveRecord::Migration
  8. def change
  9. create_table :movement_categories do |t|
  10. t.string :name
  11. t.references :user, index: true, foreign_key: true
  12.  
  13. t.timestamps null: false
  14. end
  15.  
  16. add_reference :inflows, :movement_category, index: true, foreign_key: true
  17. add_reference :outflows, :movement_category, index: true, foreign_key: true
  18.  
  19. InflowCategory.find_each do |category|
  20. inflows = Inflow.where(inflow_category_id: category.id)
  21.  
  22. new_category = if MovementCategory.where(name: category.name).any?
  23. MovementCategory.where(name: category.name).first
  24. else
  25. MovementCategory.create!(
  26. name: category.name,
  27. user_id: category.user_id,
  28. created_at: category.created_at,
  29. updated_at: category.updated_at
  30. )
  31. end
  32.  
  33. inflows.update_all(movement_category_id: new_category.id)
  34. end
  35.  
  36. OutflowCategory.find_each do |category|
  37. outflows = Outflow.where(outflow_category_id: category.id)
  38.  
  39. new_category = if MovementCategory.where(name: category.name).any?
  40. MovementCategory.where(name: category.name).first
  41. else
  42. MovementCategory.create!(
  43. name: category.name,
  44. user_id: category.user_id,
  45. created_at: category.created_at,
  46. updated_at: category.updated_at
  47. )
  48. end
  49.  
  50. outflows.update_all(movement_category_id: new_category.id)
  51. end
  52.  
  53. remove_column :inflows, :inflow_category_id
  54. remove_column :outflows, :outflow_category_id
  55.  
  56. drop_table :inflow_categories
  57. drop_table :outflow_categories
  58. end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement