Development - automatic deletion of an account that has not been verified after a certain period of time

Everything in the header, I want to be able to delete an account that has not been verified after a specified period of time .

I use:

  • Rails 3.0.5
  • Develop 1.3.4

My User model has the following attributes:

 devise :database_authenticatable, :confirmable, :registerable, :recoverable, :rememberable, :trackable, :validatable 

And my db/schema.rb

 create_table "users", :force => true do |t| t.string "email", :default => "", :null => false t.string "encrypted_password", :limit => 128, :default => "", :null => false t.string "reset_password_token" t.timestamp "reset_password_sent_at" t.timestamp "remember_created_at" t.integer "sign_in_count", :default => 0 t.timestamp "current_sign_in_at" t.timestamp "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.timestamp "created_at" t.timestamp "updated_at" t.string "confirmation_token" t.timestamp "confirmed_at" t.timestamp "confirmation_sent_at" end 

A small bonus - in my User model, I indicate the relationship between my data, for example:

 has_one :user_content, :dependent => :destroy 

I want his children to be removed as well.

Thanks to everyone who can help!


Thanks jrdi , here is the answer for a database with SQLite 3

 desc "Delete all unconfirmed users after 7 days" task :delete_unconfirmed_users => :environment do users = User.all(:conditions => 'confirmed_at is NULL AND confirmation_sent_at <= datetime(\'now\',\'-7 days\')') users.each do |user| user.destroy end end 

For PostGRE (Heroku)

 desc "Delete all unconfirmed users after 7 days" task :postgre_delete_unconfirmed_users => :environment do users = User.all(:conditions => 'confirmed_at is NULL AND confirmation_sent_at <= current_date - integer \'7\' ') users.each do |user| user.destroy end end 

See his answer for details. Thanks @ a lot to him.

+4
source share
1 answer

You can make a rake command that runs with a daemon like cron.

In this rake, you can get all unverified users ( :confirmed_at is nil ) and receive when sending a confirmation message :confirmation_sent_at

If you have users that meet the specifications of make user.destroy , and all of them must be removed correctly.

If you need an example of this rake, tell me and I will do your first aproximation.

--- Edit ---

Create delete_unconfirmed_users.rake in lib / tasks

 desc "Delete all unconfirmed users after 7 days" task :delete_unconfirmed_users => :environment do users = User.all(:conditions => 'confirmed_at is NULL AND confirmation_sent_at >= DATE_SUB(NOW(), INTERVAL 7 day)') users.each do |user| user.destroy end end 

Run rake delete_unconfirmed_users

+10
source

All Articles