diff --git a/db/migrate/20240418144517_add_volunteer_team_to_volunteers.rb b/db/migrate/20240418144517_add_volunteer_team_to_volunteers.rb new file mode 100644 index 0000000..e0eddde --- /dev/null +++ b/db/migrate/20240418144517_add_volunteer_team_to_volunteers.rb @@ -0,0 +1,30 @@ +class AddVolunteerTeamToVolunteers < ActiveRecord::Migration[7.1] + class VolunteerTeam < ActiveRecord::Base + has_and_belongs_to_many :volunteers + end + + class Volunteer < ActiveRecord::Base + belongs_to :volunteer_team + has_and_belongs_to_many :volunteer_teams + end + + def change + add_reference :volunteers, :volunteer_team, foreign_key: true + + reversible do |dir| + dir.up do + transaction do + Volunteer.all.find_each do |volunteer| + volunteer.update(volunteer_team: volunteer.volunteer_teams.first) + end + end + end + + dir.down do + # no-op + end + end + + change_column_null :volunteers, :volunteer_team_id, true + end +end