# Cookbook recipes eg - ordering categories in the recipe pull

**URL:** <https://www.ruby-forum.com/t/cookbook-recipes-eg-ordering-categories-in-the-recipe-pull/51714>\
**Category:** Rails\
**Created:** [January 6, 2006, 6:58am UTC](https://www.ruby-forum.com/t/cookbook-recipes-eg-ordering-categories-in-the-recipe-pull/51714 "2006-01-06T06:58:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [January 6, 2006, 6:58am UTC](https://www.ruby-forum.com/t/cookbook-recipes-eg-ordering-categories-in-the-recipe-pull/51714/1 "2006-01-06T06:58:25Z")

</div>

People,

I can order categories when looking at the categories list page but how  
do I change the recipe page to list the categories in alpha order in the  
recipe pull-down box?:

> **Category:**  
> \<% @categories.each do |category| %\> \> \<%= category.name %\> \<% end %\>

Thanks,

## Phil.

Philip R.

Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275)  
GPO Box 3411  
Sydney NSW 2001  
Australia  
Mobile: +61:(0)411-185-652  
Fax: +61:(0)2-8221-9599  
E-mail: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [January 6, 2006, 7:26am UTC](https://www.ruby-forum.com/t/cookbook-recipes-eg-ordering-categories-in-the-recipe-pull/51714/2 "2006-01-06T07:26:38Z")

</div>

In your controller just modify the query that loads the @categories  
variable to return the rows in the correct sort order.

Something like…

@categories = Categories.find(:all, :order=\>“name”)

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [January 6, 2006, 1:53pm UTC](https://www.ruby-forum.com/t/cookbook-recipes-eg-ordering-categories-in-the-recipe-pull/51714/3 "2006-01-06T13:53:11Z")

</div>

Philip,

Have a look at  
[http://blog.teksol.info/articles/2006/01/03/belongs\_to-user-interface-take-ii](http://blog.teksol.info/articles/2006/01/03/belongs_to-user-interface-take-ii)

It shows you

- how to order, and also
- how to validate for presence
- how to highlight for validation error

Alain

---

<div class="post-metadata">

**Author:** ![unebaguettesvp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/47e85d/32.png) [@unebaguettesvp](https://www.ruby-forum.com/u/unebaguettesvp)\
**Post date:** [January 6, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/cookbook-recipes-eg-ordering-categories-in-the-recipe-pull/51714/4 "2006-01-06T14:50:36Z")

</div>

in recipe\_controller.rb:

def show  
@recipe = Recipe.find(params[:id])  
@categories\_for\_select = Category.find(:all, :order =\> “name”).collect  
{  
|c| [c.name, c.id] }  
end

in show.rhtml:

\<%= select “recipe”, “category\_id”, @categories\_for\_select %\>

the option that = @recipe.category\_id will automatically be selected.
