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


#1

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


#2

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


#3

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


#4

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.


#5

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


#6

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.


#7

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


#8

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

Craig