# Category and List from a database

**URL:** <https://www.ruby-forum.com/t/category-and-list-from-a-database/48237>\
**Category:** Rails\
**Created:** [November 15, 2005, 10:57pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237 "2005-11-15T22:57:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 15, 2005, 10:57pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/1 "2005-11-15T22:57:18Z")

</div>

Can anyone offer help on how I’d list fields in a database that are  
related to a category(in another table?

This would be an example of what’s displayed.

Category

field1  
field2  
field3  
field4  
field5

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 16, 2005, 5:11pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/2 "2005-11-16T17:11:22Z")

</div>

jarkko wrote:

> On 16.11.2005, at 17.41, removed\_email\_address@domain.invalid wrote:
> 
> > Are you familiar with the ONLAMP tutorial “Rolling with Ruby on  
> > Rails”?  
> > I’d like to be able to just list out the recipes under the  
> > Categories on  
> > one page…instead of having to click a category link and have it  
> > list  
> > on a seperate one.
> > 
> > Have any idea how that would be done?
> 
> Not much different. Something like this:
> 
> in controller:  
> @cats = Category.find(:all, :include =\> :fields) # :all will make  
> this return an array of cats
> 
> then in view:  
> \<% for cat in @cats %\>
> 
> ### \<%= cat.title %\>
> 
> \<% for field in cat.fields %\> - \<%= field.title %\>
> \<% end %\> 
> \<% end %\>
> 
> These are pretty fundamental Ruby/Rails things, tho. You might want  
> to buy the Agile Web D. with Rails book [1]. It is  
> invaluable if you plan to use Rails at all.
> 
> //jarkko
> 
> [1] [http://www.pragmaticprogrammer.com/titles/rails/index.html](http://www.pragmaticprogrammer.com/titles/rails/index.html)

I am reading the book now actually, I’m just trying to learn some by  
figuring out some solutions.

When you write “fields”, would that be the same as “recipes” from the  
ONLamp tutorial?

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 16, 2005, 5:19pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/3 "2005-11-16T17:19:22Z")

</div>

And, would that be written in the Category controller or Recipe?  
I’m sorry for the really basic questions, but I’m very very new to the  
game.

Thanks.

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 16, 2005, 5:05pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/4 "2005-11-16T17:05:40Z")

</div>

On 16.11.2005, at 17.41, removed\_email\_address@domain.invalid wrote:

> Are you familiar with the ONLAMP tutorial “Rolling with Ruby on  
> Rails”?  
> I’d like to be able to just list out the recipes under the  
> Categories on  
> one page…instead of having to click a category link and have it  
> list  
> on a seperate one.
> 
> Have any idea how that would be done?

Not much different. Something like this:

in controller:  
@cats = Category.find(:all, :include =\> :fields) # :all will make  
this return an array of cats

then in view:  
\<% for cat in @cats %\>

### \<%= cat.title %\>
  

  
\<% for field in cat.fields %\>  
- \<%= field.title %\>
  
\<% end %\>  

  
\<% end %\>

These are pretty fundamental Ruby/Rails things, tho. You might want  
to buy the Agile Web D. with Rails book [1]. It is  
invaluable if you plan to use Rails at all.

//jarkko

[1] [http://www.pragmaticprogrammer.com/titles/rails/index.html](http://www.pragmaticprogrammer.com/titles/rails/index.html)

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 16, 2005, 5:50pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/5 "2005-11-16T17:50:54Z")

</div>

On 16.11.2005, at 18.11, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I am reading the book now actually, I’m just trying to learn some by  
> figuring out some solutions.
> 
> When you write “fields”, would that be the same as “recipes” from the  
> ONLamp tutorial?

It can be whatever you want. The point is that there is a one-to-many  
relationship between category and fields/recipes. In Rails lingo,  
category has\_many :recipes.

On 16.11.2005, at 18.19, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And, would that be written in the Category controller or Recipe?  
> I’m sorry for the really basic questions, but I’m very very new to the  
> game.
> 
> Thanks.

That would be written in the controller/view where you want to show  
that list. Categories and Recipes are models in your application,  
they’re not directly connected to any controller. So you can divide  
your app up in controllers in a way that makes the most sense to you.

//jarkko

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 16, 2005, 6:08pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/6 "2005-11-16T18:08:59Z")

</div>

Huge thanks Jarkko.  
I was able to implement what you wrote and get exactly  
what I was looking for.  
Any other materials you’d recommend reading/doing to  
get better at Ruby/Rails?

— Jarkko L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ONLamp tutorial?  
> > controller or Recipe?  
> > they’re not directly connected to any controller. So
> 
> > * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Yahoo! Mail - PC Magazine Editors’ Choice 2005

> **[\[object](https://mail.yahoo.com/)**
>
> \[object

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 16, 2005, 6:54pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/7 "2005-11-16T18:54:23Z")

</div>

On 16.11.2005, at 19.08, jonathan Mcintire wrote:

> Huge thanks Jarkko.

My pleasure.

> I was able to implement what you wrote and get exactly  
> what I was looking for.  
> Any other materials you’d recommend reading/doing to  
> get better at Ruby/Rails?

After you’ve perused the Rails book and can honestly say that you’ve  
digested it all, you’ve gone a long way. Anyway, you probably learn  
best by doing, so working through the tutorial in the book will  
probably teach you more than enough to get rolling.

//jarkko

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 16, 2005, 8:25am UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/8 "2005-11-16T08:25:35Z")

</div>

On 15.11.2005, at 23.57, Guest wrote:

> Can anyone offer help on how I’d list fields in a database that are  
> related to a category(in another table?

in controller:  
@cat = Category.find(params[:id], :include =\> :fields)

then in view:  
\<%= @cat.title %\>

\<% for field in @cat.fields %\> - \<%= field.title %\>
\<% end %\>

I assume you have “has\_many :fields” in your Category class  
definition already.

//jarkko

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [November 16, 2005, 4:41pm UTC](https://www.ruby-forum.com/t/category-and-list-from-a-database/48237/9 "2005-11-16T16:41:15Z")

</div>

jarkko wrote:

> On 15.11.2005, at 23.57, Guest wrote:
> 
> > Can anyone offer help on how I’d list fields in a database that are  
> > related to a category(in another table?
> 
> in controller:  
> @cat = Category.find(params[:id], :include =\> :fields)
> 
> then in view:  
> \<%= @cat.title %\>
> 
> \<% for field in @cat.fields %\> - \<%= field.title %\>
> \<% end %\>
> 
> I assume you have “has\_many :fields” in your Category class  
> definition already.
> 
> //jarkko

Are you familiar with the ONLAMP tutorial “Rolling with Ruby on Rails”?  
I’d like to be able to just list out the recipes under the Categories on  
one page…instead of having to click a category link and have it list  
on a seperate one.

Have any idea how that would be done?

Thanks for your help.
