Rails迁移更新现有记录,同时在另一个表中创建条目。

时间:2021-04-05 23:01:51

I am trying to set up Beta Invites for my Rails application. I have an Invitation model and a User model.

我正在为我的Rails应用程序设置Beta邀请。我有一个邀请模型和一个用户模型。

User has one invitation_id. I am validating that user must have an invitation_id and it should be unique. I want to write a migration so that I can update my existing users to have an invitation_id.

用户有一个invitation_id。我确认用户必须有一个邀请id,并且它应该是唯一的。我想写一个迁移,这样我就可以更新我现有的用户来获得一个邀请id。

Here is my invitation model:

这是我的邀请模式:

# Table name: invitations
#
#  id              :integer         not null, primary key
#  sender_id       :integer
#  recipient_email :string(255)
#  token           :string(255)
#  sent_at         :datetime
#  created_at      :datetime
#  updated_at      :datetime

To guarantee uniqueness, I will have to create records in invitation table and also assign the corresponding ids to the user records.

为了保证惟一性,我必须在邀请表中创建记录,并将相应的id分配给用户记录。

Can someone please suggest a migration for the same?

有人能建议同样的迁移吗?

Thanks in advance!

提前谢谢!

1 个解决方案

#1


7  

If you already did your migration for your invitations and users, you should really put the invitation populating in a custom rake task. Always a good practice to keep your migrations strictly for table manipulations.

如果您已经为您的邀请和用户做了迁移,那么您应该将填充在自定义rake任务中的邀请。始终保持您的迁移严格地用于表操作的良好实践。

/lib/tasks/distribute_invitations.rake

/lib/tasks/distribute_invitations.rake

namespace :db do
  desc "Run all custom tasks"
  task :import_all => [:distribute_invitations, :some_other_data_import]

  desc: "Some other data import"
  task :some_other_data_import => :environment do
    puts "insert task code here"
  end

  desc: "Give existing user's invitations"
  task :distribute_invitations => :environment do
    for user in User.all
      if user.invitation_id.nil?
        invite = Invitation.create(:sender_id => <some id>, :recipient_email => <some email>, :token => <some token>, :sent_at => Time.now)
        user.update_attribute(:invitation_id, invite.id)
        puts "Updated user #{user.id} with invitation_id #{invite.id}"
      else
        puts "User already has an invitation_id"
      end
    end
  end
end

After you do your migration to give your users table an invitation_id, you can run:

在您进行迁移以给您的用户表一个邀请的id之后,您可以运行:

rake db:distribute_invitations

and your existing users will have invitations created and associated to them through invitation_id.

您的现有用户将通过invitation_id创建并与之关联。

Or to run all your tasks you can do:

或者运行你能做的所有任务:

rake db:import_all

In this case it is very possible to just stick it in the migration with your User migrate:

在这种情况下,很有可能在用户迁移过程中使用它:

class AddInvitationID < ActiveRecord::Migration
  def self.up
    add_column :users, :invitation_id, :integer
    for user in User.all
      if user.invitation_id.nil?
        invite = Invitation.create(:sender_id => <some id>, :recipient_email => <some email>, :token => <some token>, :sent_at => Time.now)
        user.update_attribute(:invitation_id, invite.id)
        puts "Updated user #{user.id} with invitation_id #{invite.id}"
      else
        puts "User already has an invitation_id"
      end
    end
  end

  def self.down
    remove_colum :users, :invitation_id
  end
end

#1


7  

If you already did your migration for your invitations and users, you should really put the invitation populating in a custom rake task. Always a good practice to keep your migrations strictly for table manipulations.

如果您已经为您的邀请和用户做了迁移,那么您应该将填充在自定义rake任务中的邀请。始终保持您的迁移严格地用于表操作的良好实践。

/lib/tasks/distribute_invitations.rake

/lib/tasks/distribute_invitations.rake

namespace :db do
  desc "Run all custom tasks"
  task :import_all => [:distribute_invitations, :some_other_data_import]

  desc: "Some other data import"
  task :some_other_data_import => :environment do
    puts "insert task code here"
  end

  desc: "Give existing user's invitations"
  task :distribute_invitations => :environment do
    for user in User.all
      if user.invitation_id.nil?
        invite = Invitation.create(:sender_id => <some id>, :recipient_email => <some email>, :token => <some token>, :sent_at => Time.now)
        user.update_attribute(:invitation_id, invite.id)
        puts "Updated user #{user.id} with invitation_id #{invite.id}"
      else
        puts "User already has an invitation_id"
      end
    end
  end
end

After you do your migration to give your users table an invitation_id, you can run:

在您进行迁移以给您的用户表一个邀请的id之后,您可以运行:

rake db:distribute_invitations

and your existing users will have invitations created and associated to them through invitation_id.

您的现有用户将通过invitation_id创建并与之关联。

Or to run all your tasks you can do:

或者运行你能做的所有任务:

rake db:import_all

In this case it is very possible to just stick it in the migration with your User migrate:

在这种情况下,很有可能在用户迁移过程中使用它:

class AddInvitationID < ActiveRecord::Migration
  def self.up
    add_column :users, :invitation_id, :integer
    for user in User.all
      if user.invitation_id.nil?
        invite = Invitation.create(:sender_id => <some id>, :recipient_email => <some email>, :token => <some token>, :sent_at => Time.now)
        user.update_attribute(:invitation_id, invite.id)
        puts "Updated user #{user.id} with invitation_id #{invite.id}"
      else
        puts "User already has an invitation_id"
      end
    end
  end

  def self.down
    remove_colum :users, :invitation_id
  end
end