I have a rails application with a postgres db. I started with a rails model Ticket that kept track of the Event the ticket has access to, but multiple tickets can be purchased at one time. So now I have this:
Ticket
belongs_to :purchase
Event
belongs_to :purchase
Purchase
has_many :tickets
has_many :events
I'd like to use a migration wherever a ticket does not have a purchase, create a purchase and set its ticket_id to the ticket, set its event_id to the ticket's event_id, set the ticket's event_id to nil, then drop event_id from ticket.
Thoughts on how to approach this?