Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6127031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:27:13+00:00 2026-05-23T16:27:13+00:00

I am trying to set up Beta Invites for my Rails application. I have

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:27:13+00:00Added an answer on May 23, 2026 at 4:27 pm

    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

    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:

    rake db:distribute_invitations
    

    and your existing users will have invitations created and associated to them through 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a viral pre-beta invitation application as one can see
Im trying set the single table inheritance model type in a form. So i
Trying to set it so if a certain condition is met then one of
I'm using the Microsoft Logging Application Block (version 5 beta 2) and I'm trying
I have been trying to set up a PHP based SOAP client to connect
I have a little sample application I was working on trying to get some
I am using Ubuntu 12.04 Beta and Vim that has come with it. I
I'm trying to set up a web application for the first time on Amazon
I have a Silverlight 4 Beta application where I'd like to use the SharePoint
I have a databound Silverlight DataGrid control that I am trying to sort. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.