# Don't destroy last user

**URL:** <https://www.ruby-forum.com/t/dont-destroy-last-user/70109>\
**Category:** Rails\
**Created:** [February 18, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/dont-destroy-last-user/70109 "2006-02-18T18:30:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [February 18, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/dont-destroy-last-user/70109/1 "2006-02-18T18:30:49Z")

</div>

How would I stop the last user being deleted.  
The following code doesn’t work.

before\_destroy :dont\_destroy\_last\_user

# Don’t delete user if it the last one

def dont\_destroy\_last\_User  
raise “Can’t destroy last user” if User.length \< 1  
end

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [February 18, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/dont-destroy-last-user/70109/2 "2006-02-18T18:44:22Z")

</div>

On 18-feb-2006, at 18:30, Alexander wrote:

> How would I stop the last user being deleted.  
> The following code doesn’t work.

before\_destroy :check\_if\_last\_user

private  
def check\_if\_last\_user  
raise “Last user can’t be deleted” if self.class.count == 1  
end

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [February 18, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/dont-destroy-last-user/70109/3 "2006-02-18T18:44:43Z")

</div>

Alexander wrote:

> How would I stop the last user being deleted.  
> The following code doesn’t work.
> 
> before\_destroy :dont\_destroy\_last\_user
> 
> # Don’t delete user if it the last one
> 
> def dont\_destroy\_last\_User  
> raise “Can’t destroy last user” if User.length \< 1  
> end  
> Change to:
> 
> # Don’t delete user if it the last one
> 
> def dont\_destroy\_last\_User  
> raise “Can’t destroy last user” if User.count == 1  
> end

Untested,but should work

joey\_\_

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [February 18, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/dont-destroy-last-user/70109/4 "2006-02-18T19:06:12Z")

</div>

Julian ‘Julik’ Tarkhanov wrote:

> On 18-feb-2006, at 18:30, Alexander wrote:
> 
> > How would I stop the last user being deleted.  
> > The following code doesn’t work.
> 
> before\_destroy :check\_if\_last\_user
> 
> private  
> def check\_if\_last\_user  
> raise “Last user can’t be deleted” if self.class.count == 1  
> end

Thanks

Also ‘raise’ creates quite a ugly error message. Is there any way of  
redirecting and using flash instead?

Alex

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [February 18, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/dont-destroy-last-user/70109/5 "2006-02-18T19:14:55Z")

</div>

On 18-feb-2006, at 19:06, Alexander wrote:

> > ```
> > raise "Last user can't be deleted" if self.class.count == 1
> > 
> > ```
> > 
> > end
> 
> Thanks
> 
> Also ‘raise’ creates quite a ugly error message. Is there any way of  
> redirecting and using flash instead?

I think the neat way would be with an exception catcher

in the model:

class LastUserCantBeDeleted \< RuntimeError; end;

… raise LastUserCantBeDeleted if self.class.count == 1

in the controller

begin  
@user.destroy!  
rescue LastUserCantBeDeleted  
flash[:error] = "This is the last one’  
redirect\_to :referer # or something like that  
end

A less neat way would be to return false from the method and thus  
stop the callback chain, and then

unless @user.destroy!  
flash[:error] = “This is the last one”

# … etc

end

---

<div class="post-metadata">

**Author:** ![alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alexander](https://www.ruby-forum.com/u/alexander)\
**Post date:** [February 19, 2006, 12:03pm UTC](https://www.ruby-forum.com/t/dont-destroy-last-user/70109/6 "2006-02-19T12:03:00Z")

</div>

Julian ‘Julik’ Tarkhanov wrote:

> flash[:error] = "This is the last one’  
> end  
> This will not work if you have two independent FCGI/SCGI processes  
> trying  
> to delete the last user at the same time. Might sound bit extreme, but  
> its  
> quite possible for example if the user clicks rapidly the submit button  
> twice.

You’ll need to wrap the deletion and the counting of users in a  
transaction. The first beta release of AWDWR had similar code for  
preventing the deletion of the last administrator. Revised version was  
simpler not allowing to delete ‘Dave’ user. That’s another alternative.
