cat
cat

Reputation: 749

How can I make only confirmed user as valid user at default?

I'm using Devise and its confirm option.
As you know, a user won't be activated unless the user clicks on the link in Confirmation mail.

The problem I have here is, we can see all the members including the user who hasn't confirmed yet!

@users = User.all will fetch all the users! I don't want to include un-confirmed users.

Is there any technique to ignore those who haven't confirmed yet?
Adding something in User model will be the best!

Please help me:)

Upvotes: 1

Views: 96

Answers (3)

Nazar
Nazar

Reputation: 1509

I'd recommend adding a scope to your User model for consistent access:

class User

  def self.unconfirmed
    where('users.confirmed_at is null')
  end

  def self.confirmed
    where('users.confirmed_at is not null')
  end

end

Then in controllers (or other models), this list can be accessed as:

@users = User.confirmed

The beauty of scopes is that they could be chained further:

@unformed_males = User.confirmed.where(:sex => 'male').order(:height)

You get the idea ;)

HTH

Upvotes: 1

Dennis Hackethal
Dennis Hackethal

Reputation: 14275

Devise will give you the attribute confirmed_at. You can use:

@user = User.where('confirmed_at IS NOT NULL')

And, if I am not mistaken, after confirmation Devise sets the confirmation_token to NULL so that it can't be used again, so you could also do:

@user = User.where('confirmation_token IS NULL')

Upvotes: 1

Rahul Tapali
Rahul Tapali

Reputation: 10137

Devise adds confirmed_at column to your users table. So you can use as follows:

     @users = User.all(:conditions => ["confirmed_at is not null"])

Upvotes: 0

Related Questions