Created
February 6, 2012 02:02
-
-
Save Catharz/1748925 to your computer and use it in GitHub Desktop.
Rails Migration from 1-M to M-M Relationship
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateInstances < ActiveRecord::Migration | |
def self.up | |
# Create the new instances table | |
create_table :instances do |t| | |
t.references :zone | |
t.references :raid | |
t.datetime :start_time | |
t.datetime :end_time | |
t.timestamps | |
end | |
# Create the drops.instance relationship | |
add_column :drops, :instance_id, :integer | |
Drop.reset_column_information | |
# Create the instances.players relationship | |
create_table :instances_players, :id => false do |t| | |
t.references :instance | |
t.references :player | |
end | |
end | |
def self.down | |
remove_column :drops, :instance_id | |
Drop.reset_column_information | |
drop_table :instances_players | |
drop_table :instances | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PopulateInstances < ActiveRecord::Migration | |
def self.up | |
Raid.all.each do |raid| | |
instance = Instance.create(:zone => raid.zone, | |
:start_time => raid.start_time, | |
:end_time => raid.end_time) | |
Drop.find_all_by_raid_id(raid.id).each do |drop| | |
drop.instance_id = instance.id | |
drop.save | |
end | |
raid.players.each { |player| instance.players << player } | |
instance.save | |
end | |
end | |
def self.down | |
Instance.delete_all | |
Drop.each.all do |drop| | |
drop.instance_id = nil | |
drop.save | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RestructureRaids < ActiveRecord::Migration | |
def self.up | |
Raid.delete_all | |
Instance.all.each do |instance| | |
raid = Raid.find_by_raid_date(instance.start_time.to_date) | |
raid ||= Raid.create!(:raid_date => instance.start_time.to_date) | |
instance.raid_id = raid.id | |
instance.save | |
end | |
remove_column :raids, :zone_id | |
remove_column :raids, :start_time | |
remove_column :raids, :end_time | |
Raid.reset_column_information | |
remove_column :drops, :raid_id | |
Drop.reset_column_information | |
drop_table :players_raids | |
end | |
def self.down | |
add_column :drops, :raid_id, :integer | |
Drop.reset_column_information | |
create_table :players_raids, :id => false do |t| | |
t.integer :player_id | |
t.integer :raid_id | |
end | |
add_column :raids, :zone_id, :integer | |
add_column :raids, :start_time, :datetime | |
add_column :raids, :end_time, :datetime | |
Raid.reset_column_information | |
Raid.delete_all | |
Instance.all.each do |instance| | |
raid = Raid.create!(:zone => instance.zone, | |
:raid_date => instance.start_time.to_date, | |
:start_time => instance.start_time, | |
:end_time => instance.end_time) | |
raid.players << instance.players | |
raid.drops << instance.drops | |
raid.save | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Raid < ActiveRecord::Base | |
belongs_to :zone | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Raid < ActiveRecord::Base | |
has_many :zones, :through => :raids_zones | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Zone < ActiveRecord::Base | |
has_many :raids | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Zone < ActiveRecord::Base | |
has_many :raids, :through => :raids_zones | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment