# The cookbook - category delete?

**URL:** <https://www.ruby-forum.com/t/the-cookbook-category-delete/104876>\
**Category:** Rails\
**Created:** [May 23, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/the-cookbook-category-delete/104876 "2007-05-23T14:12:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@marko](https://www.ruby-forum.com/u/marko)\
**Post date:** [May 23, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/the-cookbook-category-delete/104876/1 "2007-05-23T14:12:18Z")

</div>

How can I delete category and set all recipes linked to that category to  
“no category”?

The tutorial at O’Reilly says nothing about that…

---

<div class="post-metadata">

**Author:** ![marko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@marko](https://www.ruby-forum.com/u/marko)\
**Post date:** [May 23, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/the-cookbook-category-delete/104876/2 "2007-05-23T14:23:56Z")

</div>

Hi, you’ll simply have to change

---

<div class="post-metadata">

**Author:** ![marko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@marko](https://www.ruby-forum.com/u/marko)\
**Post date:** [May 23, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/the-cookbook-category-delete/104876/3 "2007-05-23T14:40:08Z")

</div>

Conrad T. wrote:

> Hi, you’ll simply have to change

🙂 So do I have to make a new Category with name ‘no category’ and than  
link all recipes to new category?

What if I have Author of recipe…

table Authors with fields:

id  
first\_name  
last\_name  
city  
state  
street  
zip  
phone

Now how do I set “no author” here?

---

<div class="post-metadata">

**Author:** ![marko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@marko](https://www.ruby-forum.com/u/marko)\
**Post date:** [May 23, 2007, 4:00pm UTC](https://www.ruby-forum.com/t/the-cookbook-category-delete/104876/4 "2007-05-23T16:00:15Z")

</div>

Hi Marko,

Marko wrote:

> Conrad T. wrote:
> 
> > Hi, you’ll simply have to change
> 
> 🙂 So do I have to make a new Category with name  
> ‘no category’ and than link all recipes to new category?

Yes. Because the belongs\_to relationship requires that a recipe must  
have a  
category.

> What if I have Author of recipe…

> phone
> 
> Now how do I set “no author” here?

The answer depends on the relationship you want to maintain between the  
various entities. Do you want to make sure that you every Recipe has an  
Author? What about having Authors hanging around with no contributions?  
The table definition above has no foreign keys and so, as it stands, you  
currently have no way to link it to Recipes. These are basic database  
design questions/concepts that really have nothing to do with Rails.  
You  
might want to Google ‘database design’ and spend a little time getting  
your  
arms around the basics.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![marko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@marko](https://www.ruby-forum.com/u/marko)\
**Post date:** [May 23, 2007, 3:59pm UTC](https://www.ruby-forum.com/t/the-cookbook-category-delete/104876/5 "2007-05-23T15:59:13Z")

</div>

what i did was make a category titled category and make it so its not  
editable or deletable by the user.

that way, there will be at least one category for the user to keep notes  
attached to “categories has many note, notes belong to category”  
relationship.

Marko wrote:

> How can I delete category and set all recipes linked to that category to  
> “no category”?
> 
> The tutorial at O’Reilly says nothing about that…

---

<div class="post-metadata">

**Author:** ![marko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@marko](https://www.ruby-forum.com/u/marko)\
**Post date:** [May 23, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/the-cookbook-category-delete/104876/6 "2007-05-23T16:13:18Z")

</div>

i believe what you want to do is:

Recipe \< AR::Base  
belongs\_to :category  
belongs\_to :author  
end

Category \< AR::Base  
has\_many :recipes, :dependent =\> :nullify  
end

Author \< AR::Base  
has\_many :recipes, :dependent =\> :nullify  
end

what this does is if you delete/destroy a category or author, the  
recipe’s foreign keys will be set to null, which in effect says that  
the recipe has no category or author, depending on which you  
deleted/destroyed.
