# How do change catalog before catalog is delted?

**URL:** <https://www.ruby-forum.com/t/how-do-change-catalog-before-catalog-is-delted/142185>\
**Category:** Rails\
**Created:** [July 4, 2008, 9:16am UTC](https://www.ruby-forum.com/t/how-do-change-catalog-before-catalog-is-delted/142185 "2008-07-04T09:16:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [July 4, 2008, 9:16am UTC](https://www.ruby-forum.com/t/how-do-change-catalog-before-catalog-is-delted/142185/1 "2008-07-04T09:16:37Z")

</div>

In my rails application,there are two models:post and catalog.  
One post has a catalog,and one catalog has many posts.  
When I delete a catalog,the posts belongs to the catalog will not be  
shown normal,for it’s catalog is no existed.

Now I want to create(if the ‘Defalut catalog’ is not existed) a  
‘Defalut catalog’ in catalogs table,and make the posts’s catalog belongs  
to it,before I delete a catalog.

I just know there is ‘before\_destory’ in ActiveRecord.But I don’t know  
how to do it.

Thanks for any replies.

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [July 4, 2008, 9:40am UTC](https://www.ruby-forum.com/t/how-do-change-catalog-before-catalog-is-delted/142185/2 "2008-07-04T09:40:41Z")

</div>

There are several ways to do this, before\_destroy is a good option

just do (in model)  
before\_destroy : move\_to\_default

def move\_to\_default  
#…code that sets a posts to the default…

# something like this

@default\_catalog \<\< posts  
end

I see a slight disadvantage here (depending on your app this may be  
wrong)  
You’ll have a default catalog araound all the time and have to handle  
it,  
avoid that it’s shown in the wrong places, where only “normal”  
catalogs should show  
You could simple nullify the associations of the posts and handle all  
posts if a catalog\_id of NULL as belonging to default catalog.  
This would save you an extra dummy record in the catalog table.

A minor advantage of this would be, that Rails could handle this by  
itself  
in catalog model:  
has\_many :posts, :dependent =\> :nullify  
would be all you need and  
Post.find(:all, :conditions =\> {catalog\_id =\> nil})  
would give you the abandoned posts as easy as the default catalog

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [July 4, 2008, 10:19am UTC](https://www.ruby-forum.com/t/how-do-change-catalog-before-catalog-is-delted/142185/3 "2008-07-04T10:19:21Z")

</div>

I would follow the second option as Thorsten says, having dummy  
records that do noting helps no one. For example, in our apps, when we  
want to recreate this sort of behaviour, we do it straight in the  
model. so for example, records that have no name always return the  
same string defined in a constant. or users that dont have an avatar  
nearly always return their grvatar url if they have one, else they  
return our default avatar…etc etc

j
