I am trying to set up Beta Invites for my Rails application. I have an Invitation model and a User model.
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.
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.
Can someone please suggest a migration for the same?
Thanks in advance!
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.
/lib/tasks/distribute_invitations.rake
After you do your migration to give your users table an invitation_id, you can run:
and your existing users will have invitations created and associated to them through invitation_id.
Or to run all your tasks you can do:
In this case it is very possible to just stick it in the migration with your User migrate: