Category and List from a database

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

jarkko wrote:

On 16.11.2005, at 17.41, [email protected] 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

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?

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.

On 16.11.2005, at 17.41, [email protected] 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

On 16.11.2005, at 18.11, [email protected] [email protected]
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, [email protected]
[email protected] 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

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. [email protected] wrote:

ONLamp tutorial?
controller or Recipe?
they’re not directly connected to any controller. So


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Yahoo! Mail - PC Magazine Editors’ Choice 2005

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

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

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.