Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>/lib/tasks/distribute_invitations.rake</p> <pre><code>namespace :db do desc "Run all custom tasks" task :import_all =&gt; [:distribute_invitations, :some_other_data_import] desc: "Some other data import" task :some_other_data_import =&gt; :environment do puts "insert task code here" end desc: "Give existing user's invitations" task :distribute_invitations =&gt; :environment do for user in User.all if user.invitation_id.nil? invite = Invitation.create(:sender_id =&gt; &lt;some id&gt;, :recipient_email =&gt; &lt;some email&gt;, :token =&gt; &lt;some token&gt;, :sent_at =&gt; 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 </code></pre> <p>After you do your migration to give your users table an invitation_id, you can run:</p> <pre><code>rake db:distribute_invitations </code></pre> <p>and your existing users will have invitations created and associated to them through invitation_id.</p> <p>Or to run all your tasks you can do:</p> <pre><code>rake db:import_all </code></pre> <p>In this case it is very possible to just stick it in the migration with your User migrate:</p> <pre><code>class AddInvitationID &lt; 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 =&gt; &lt;some id&gt;, :recipient_email =&gt; &lt;some email&gt;, :token =&gt; &lt;some token&gt;, :sent_at =&gt; 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload