0

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?

4

1 回答 1

0

以下是如何在 Rails 中生成 rake 任务:http ://railsguides.net/2012/03/14/how-to-generate-rake-task/

您更新历史记录的 rake 任务应如下所示:

# lib/tasks/something.rake
desc "Update old stuff"
task :my_task1 => :environment do
  tickets = Ticket.where("purchase_id IS NULL")
  tickets.each do |ticket|
    ticket.purchase.create!([attributes])
    ...[more stuff]...
  end
end

在命令上运行 rake 任务,例如:

$ bundle exec rake my_task1

我无法 100% 遵循您的应用程序的逻辑,但希望这会让您朝着正确的方向前进。

于 2013-08-06T23:57:44.303 回答