Forum: Ruby on Rails New to rails - scaffold command , how to get related items?

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.
81a3fc5c10a93d34ee0f25eb1e4227a1?d=identicon&s=25 Will Jessup (wjessup)
on 2006-04-15 23:57
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 %>
    <td><%=h item.send(column.name) %></td>
  <% end %>
"

And putting

"
<td><%h item.Category ? item.Category.name : "no category" %></td>
or
<td><% item.Category.name %></td>
"
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-16 00:08
(Received via mailing list)
On Sat, 2006-04-15 at 23:57 +0200, Will Jessup wrote:
>
> "
>   <% for column in Item.content_columns %>
>     <td><%=h item.send(column.name) %></td>
>   <% end %>
> "
>
> And putting
>
> "
> <td><%h item.Category ? item.Category.name : "no category" %></td>
----
try
<%=h item.Category ? item.Category.name : "no category" %>
----
> or
> <td><% item.Category.name %></td>
----
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-16 00:36
(Received via mailing list)
On Sat, 2006-04-15 at 15:05 -0700, Craig White wrote:
> > Now the templates look like
> > <td><%h item.Category ? item.Category.name : "no category" %></td>
> > "
> > 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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-16 00:45
(Received via mailing list)
On Apr 15, 2006, at 3:34 PM, Craig White 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 Mornini
81a3fc5c10a93d34ee0f25eb1e4227a1?d=identicon&s=25 Will Jessup (wjessup)
on 2006-04-16 04:15
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 Mornini wrote:
> On Apr 15, 2006, at 3:34 PM, Craig White 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 Mornini
81a3fc5c10a93d34ee0f25eb1e4227a1?d=identicon&s=25 Will Jessup (wjessup)
on 2006-04-16 04:33
Oh, as a future note - One of my category_id fields in the categories
table was blank, this caused an error as well.
Re:
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-16 05:02
(Received via mailing list)
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 Thomas

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/...

Craig
Re:
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-04-16 05:38
(Received via mailing list)
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
This topic is locked and can not be replied to.