Forum: Ruby on Rails Category and List from a database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Guest (Guest)
on 2005-11-15 23:57
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 (Guest)
on 2005-11-16 09:25
(Received via mailing list)
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 %>
<ul>
<% for field in @cat.fields %>
   <li><%= field.title %></li>
<% end %>
</ul>

I assume you have "has_many :fields" in your Category class
definition already.

//jarkko
Guest (Guest)
on 2005-11-16 17:41
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 %>
> <ul>
> <% for field in @cat.fields %>
>    <li><%= field.title %></li>
> <% end %>
> </ul>
>
> 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.
Jarkko L. (Guest)
on 2005-11-16 18:05
(Received via mailing list)
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 %>
	<h3><%= cat.title %></h3>
	<ul>
	<% for field in cat.fields %>
	   <li><%= field.title %></li>
	<% end %>
	</ul>
<% 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
<> (Guest)
on 2005-11-16 18:11
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 %>
> 	<h3><%= cat.title %></h3>
> 	<ul>
> 	<% for field in cat.fields %>
> 	   <li><%= field.title %></li>
> 	<% end %>
> 	</ul>
> <% 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?
<> (Guest)
on 2005-11-16 18:19
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.
Jarkko L. (Guest)
on 2005-11-16 18:50
(Received via mailing list)
On 16.11.2005, at 18.11, <removed_email_address@domain.invalid> 
<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>
<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
Jm M. (Guest)
on 2005-11-16 19:08
(Received via mailing list)
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> 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
>







__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
Jarkko L. (Guest)
on 2005-11-16 19:54
(Received via mailing list)
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
This topic is locked and can not be replied to.