# New to rails - scaffold command , how to get related items?

**URL:** <https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906>\
**Category:** Rails\
**Created:** [April 15, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906 "2006-04-15T23:57:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 15, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906/1 "2006-04-15T23:57:23Z")

</div>

Ahoy,

I was able to follow the tutorials and build a simple to-do list program  
with Items and Categories and have the related tables link up w/ the  
default “scaffold :Items” command plus custom defenitions.

Then I erased all of that and tried rebuilding the program using the  
ruby script/generate scaffold Item (and category) command.

Now the templates look like

"  
\<% for column in Item.content\_columns %\>

\<%=h item.send(column.name) %\>  
\<% end %\>  
"

And putting

"

\<%h item.Category ? item.Category.name : "no category" %\> or \<% item.Category.name %\> " both don't work.

So, how exactly do I show related fields’ information now?  
is there a slick way to do this automagically? Shouldn’t rails know that  
since there is a has\_many and belongs\_to in the model that these things  
may link up?

Will

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 12:08am UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906/2 "2006-04-16T00:08:36Z")

</div>

On Sat, 2006-04-15 at 23:57 +0200, Will Jessup wrote:

> "  
> \<% for column in Item.content\_columns %\>
> 
> \<%=h item.send(column.name) %\>  
> \<% end %\>  
> "
> 
> And putting
> 
> "
> 
> \<%h item.Category ? item.Category.name : "no category" %\>

* * *

## try \<%=h item.Category ? item.Category.name : “no category” %\>

> or
> 
> \<% item.Category.name %\>

* * *

## try \<%= item.Category.name %\>

> "  
> both don’t work.
> 
> So, how exactly do I show related fields’ information now?  
> is there a slick way to do this automagically? Shouldn’t rails know that  
> since there is a has\_many and belongs\_to in the model that these things  
> may link up?

* * *

It does

Craig

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 12:36am UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906/3 "2006-04-16T00:36:06Z")

</div>

On Sat, 2006-04-15 at 15:05 -0700, Craig W. wrote:

> > Now the templates look like
> > 
> > \<%h item.Category ? item.Category.name : "no category" %\> " both don't work.
> > 
> > So, how exactly do I show related fields’ information now?  
> > is there a slick way to do this automagically? Shouldn’t rails know that  
> > since there is a has\_many and belongs\_to in the model that these things  
> > may link up?
> 
> * * *
> 
> It does

* * *

now that I’m looking at this, I don’t think Category the class should be  
used (capital “C”)

if category belongs to item, then you should only need to use…

\<%= item.category.name %\>

Craig

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 12:45am UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906/4 "2006-04-16T00:45:21Z")

</div>

On Apr 15, 2006, at 3:34 PM, Craig W. wrote:

> now that I’m looking at this, I don’t think Category the class  
> should be  
> used (capital “C”)
> 
> if category belongs to item, then you should only need to use…
> 
> \<%= item.category.name %\>

If item has\_many categories, then you need:

item.categories

but you cannot say:

item.categories.name

because categories is plural, and therefore will return an array of  
categories, even if there’s only one.

So, you might want to try:

item.categories.collect { |c| c.name }.join(’, ')

This code iterates through the array returned by item.categories,  
an returns a new array of names which is then iterated over again  
by join, which contatenates them with comma-space between each  
pair.

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 4:33am UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906/5 "2006-04-16T04:33:11Z")

</div>

Oh, as a future note - One of my category\_id fields in the categories  
table was blank, this caused an error as well.

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 4:15am UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906/6 "2006-04-16T04:15:07Z")

</div>

Craig- thanks, all I needed was the “=” (omfg).

Tom, yea that’s not the problem.

I was thinking, though - if you put has\_many: and belongs\_to, shouldn’t  
the auto-scaffold know to connect those things automagically (including  
creating select boxes) in the list/edit/new ? or is that too much  
automation?

2nd -  
\<%= item.category.name %\>

I had the “C” capitalized because that’s how it is in the model, i’m not  
sure what the standard is. Also, has\_many is plural and belongs\_to is  
singular. this is normal right? as in- not every association in the  
model is either singular/plural, but depending on context (that’s what  
i’m doing now, just double checking)

Will

THanks much!

Tom M. wrote:

> On Apr 15, 2006, at 3:34 PM, Craig W. wrote:
> 
> > now that I’m looking at this, I don’t think Category the class  
> > should be  
> > used (capital “C”)
> > 
> > if category belongs to item, then you should only need to use…
> > 
> > \<%= item.category.name %\>
> 
> If item has\_many categories, then you need:
> 
> item.categories
> 
> but you cannot say:
> 
> item.categories.name
> 
> because categories is plural, and therefore will return an array of  
> categories, even if there’s only one.
> 
> So, you might want to try:
> 
> item.categories.collect { |c| c.name }.join(’, ')
> 
> This code iterates through the array returned by item.categories,  
> an returns a new array of names which is then iterated over again  
> by join, which contatenates them with comma-space between each  
> pair.
> 
> –  
> – Tom M.

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 5:38am UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906/7 "2006-04-16T05:38:30Z")

</div>

On Sun, 2006-04-16 at 04:15 +0200, Will Jessup wrote:

> 2nd -  
> \<%= item.category.name %\>
> 
> I had the “C” capitalized because that’s how it is in the model, i’m not  
> sure what the standard is. Also, has\_many is plural and belongs\_to is  
> singular. this is normal right? as in- not every association in the  
> model is either singular/plural, but depending on context (that’s what  
> i’m doing now, just double checking)

* * *

I neglected to comment on singular/plural.

It is the natural context that defines the ‘defaults’.

has\_many :plural  
has\_one :singular

Class & model refer to a single record.

Craig

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 5:02am UTC](https://www.ruby-forum.com/t/new-to-rails-scaffold-command-how-to-get-related-items/57906/8 "2006-04-16T05:02:01Z")

</div>

On Sun, 2006-04-16 at 04:15 +0200, Will Jessup wrote:

> 2nd -  
> \<%= item.category.name %\>
> 
> I had the “C” capitalized because that’s how it is in the model, i’m not  
> sure what the standard is. Also, has\_many is plural and belongs\_to is  
> singular. this is normal right? as in- not every association in the  
> model is either singular/plural, but depending on context (that’s what  
> i’m doing now, just double checking)

* * *

I think it’s a ruby thing - you might want to pick up a copy of the  
‘PickAxe’ book aka “Programming Ruby” by Dave T.

anyway, I think the early version is online and you can read about  
variables/classes etc. here…

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut\_classes.html](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html)

Craig
