paranoic

Nonoptimal many-to-many associations

Jan 11th, 2014
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.64 KB | None | 0 0
  1. #Nonoptimal many-to-many associations
  2.  
  3. #app/models/train.rb
  4.  
  5. class Train < ActiveRecord::Base
  6.   has_many :train_locations, dependent: :destroy
  7.   has_many :locations, through: :train_locations
  8. end
  9.  
  10. #app/models/train_location.rb
  11.  
  12. class TrainLocation < ActiveRecord::Base
  13.   belongs_to :train
  14.   belongs_to :location
  15. end
  16.  
  17. #app/models/copter.rb
  18.  
  19. class Copter < ActiveRecord::Base
  20.   has_many :copter_locations, dependent: :destroy
  21.   has_many :locations, through: :copter_locations
  22. end
  23.  
  24. #app/models/copter_location.rb
  25.  
  26. class CopterLocation < ActiveRecord::Base
  27.   belongs_to :copter
  28.   belongs_to :location
  29. end
  30.  
  31. #app/models/truck.rb
  32.  
  33. class Truck < ActiveRecord::Base
  34.   has_many :truck_locations, dependent: :destroy
  35.   has_many :locations, through: :truck_locations
  36. end
  37.  
  38. #app/models/truck_location.rb
  39.  
  40. class TruckLocation < ActiveRecord::Base
  41.   belongs_to :truck
  42.   belongs_to :location
  43. end
  44.  
  45. #app/models/ship.rb
  46.  
  47. class Ship < ActiveRecord::Base
  48.   has_many :ship_locations, dependent: :destroy
  49.   has_many :locations, through: :ship_locations
  50. end
  51.  
  52. #app/models/ship_location.rb
  53.  
  54. class ShipLocation < ActiveRecord::Base
  55.   belongs_to :ship
  56.   belongs_to :location
  57. end
  58.  
  59. #app/models/location.rb
  60.  
  61. class Location < ActiveRecord::Base
  62.   has_many :train_locations, dependent: :destroy
  63.   has_many :copter_locations, dependent: :destroy  
  64.   has_many :truck_locations, dependent: :destroy
  65.   has_many :ship_locations, dependent: :destroy  
  66.  
  67.   has_many :trains, :through => :train_locations
  68.   has_many :copters, :through => :copter_locations
  69.   has_many :trucks, :through => :truck_locations
  70.   has_many :ships, :through => :ship_locations
  71. end
Advertisement
Add Comment
Please, Sign In to add comment