Turn the argument into the target number of unused invitations for each user, thus not generating more invitations for users who already have a sufficient amount of unused ones.
23 lines
624 B
Ruby
23 lines
624 B
Ruby
namespace :invitations do
|
|
desc "Generate invitations for all users"
|
|
task :generate_for_all_users, [:amount_per_user] => :environment do |t, args|
|
|
count = 0
|
|
|
|
User.all.each do |user|
|
|
amt_to_create = args[:amount_per_user].to_i - user.invitations.unused.count
|
|
next unless amt_to_create > 0
|
|
amt_created = 0
|
|
|
|
amt_to_create.times do
|
|
user.invitations << Invitation.create(user: user)
|
|
count += 1
|
|
amt_created += 1
|
|
end
|
|
|
|
puts "Created #{amt_created} new invitations for #{user.address}"
|
|
end
|
|
|
|
puts "---\nCreated #{count} new invitations overall"
|
|
end
|
|
end
|