# Two destroy methods with different behavior for the model

**URL:** <https://www.ruby-forum.com/t/two-destroy-methods-with-different-behavior-for-the-model/168554>\
**Category:** Rails\
**Created:** [May 22, 2009, 10:10am UTC](https://www.ruby-forum.com/t/two-destroy-methods-with-different-behavior-for-the-model/168554 "2009-05-22T10:10:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![term](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/34f0e0/32.png) [@term](https://www.ruby-forum.com/u/term)\
**Post date:** [May 22, 2009, 10:10am UTC](https://www.ruby-forum.com/t/two-destroy-methods-with-different-behavior-for-the-model/168554/1 "2009-05-22T10:10:29Z")

</div>

I want to have two destroy methods for my model.

First method must be a “classic” destroy method — find a record and  
destroy it. “Delete Forever” link in the view.  
Second method must find a record and mark it as deleted (or as moved  
to trash). “Move to Trash” link in the view.

How can I do this? Should I just use model.destroy for the first case  
and write custom method for the second? In this case how can I make a  
“Move to Trash” link for the second method in the view inaccessible  
for spiders?

Or can I pass an argument for destroy method from view and depending  
on that argument do a “classic” destroy or just mark the record as  
deleted?

Or even something else?

---

<div class="post-metadata">

**Author:** ![term](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/34f0e0/32.png) [@term](https://www.ruby-forum.com/u/term)\
**Post date:** [May 22, 2009, 10:15am UTC](https://www.ruby-forum.com/t/two-destroy-methods-with-different-behavior-for-the-model/168554/2 "2009-05-22T10:15:45Z")

</div>

On May 22, 9:09 am, Denis K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> for spiders?

I would go this way. Whenever you display records to the end user  
you’ll need to check for that flag ( named\_scope and/or default\_scope  
may be useful here).

Fred

---

<div class="post-metadata">

**Author:** ![term](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/34f0e0/32.png) [@term](https://www.ruby-forum.com/u/term)\
**Post date:** [May 22, 2009, 10:55am UTC](https://www.ruby-forum.com/t/two-destroy-methods-with-different-behavior-for-the-model/168554/3 "2009-05-22T10:55:35Z")

</div>

Thank you, Frederick.

Can you give me more details? Some sample code would be useful for me.

---

<div class="post-metadata">

**Author:** ![term](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/34f0e0/32.png) [@term](https://www.ruby-forum.com/u/term)\
**Post date:** [May 23, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/two-destroy-methods-with-different-behavior-for-the-model/168554/4 "2009-05-23T21:21:35Z")

</div>

Denis,

I did this sort of thing with user accounts a while ago… I had set up  
a site that needed to mark users as ‘inactive’ if they ever decided to  
close their account.

Your issue with web crawlers seeing the Move to Trash link makes me  
think this page is public and not member-only accessible? If so, I  
can’t think of any way to tell a spider to not look at particular  
parts of the text (maybe there is a way, i’ve always done a  
traditional approach and told the spider to leave my whole page  
alone). Either way, on to a possible solution:

The two actions I would have are:  
(already in your code) desotry - this action houses the traditional  
rails destroy method that will remove the record from the table and  
any dependencies.  
(need to add) move\_to\_trash - the new action that will call the  
move\_to\_tash method we will create on the object in view

Explanation:  
You should not modify the destroy action and following rails  
conventions and good programming practice I would not pass a parameter  
into a destroy action and then decide what kind of ‘destroy’ to  
execute. Rather, let’s create a second action called move\_to\_trash  
with the same access privileges as the destroy. The reason we do this  
is because logically our two actions are doing very different things,  
one is destroying a sql record and the other is changing an attribute  
on an object.

So we add a move\_to\_trash method to the model and subsequently we will  
want a new column on our model moved\_to\_trash (boolean). Now the  
move\_to\_trash action will call the model.move\_to\_trash and then that  
method will called a self.update\_attribute(:moved\_to\_trash, true).

[[Alternatively, this is where I like to encourage using enums in your  
database and have your model have states. It could be very clean to  
have a column “status” and then you  
have :moved\_to\_trash, :inbox, :other\_cool\_states ]]

Hopefully you find this helpful. If you provide more details about  
your particular situation we can work out a solution that fits.

---

<div class="post-metadata">

**Author:** ![term](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/34f0e0/32.png) [@term](https://www.ruby-forum.com/u/term)\
**Post date:** [May 29, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/two-destroy-methods-with-different-behavior-for-the-model/168554/5 "2009-05-29T13:31:32Z")

</div>

Thank you, Mike.

I’ve already made two different methods, just as you explained.
