Will Jessup
2006-Apr-15 21:57 UTC
[Rails] New to rails - scaffold command , how to get related items?
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 -- Posted via http://www.ruby-forum.com/.
Craig White
2006-Apr-15 22:06 UTC
[Rails] New to rails - scaffold command , how to get related items?
On Sat, 2006-04-15 at 23:57 +0200, Will Jessup wrote:> 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>---- 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
Craig White
2006-Apr-15 22:34 UTC
[Rails] New to rails - scaffold command , how to get related items?
On Sat, 2006-04-15 at 15:05 -0700, Craig White wrote:> On Sat, 2006-04-15 at 23:57 +0200, Will Jessup wrote: > > 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> > ---- > 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---- 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
Tom Mornini
2006-Apr-15 22:42 UTC
[Rails] New to rails - scaffold command , how to get related items?
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
Will Jessup
2006-Apr-16 02:15 UTC
[Rails] Re: New to rails - scaffold command , how to get related ite
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-- Posted via http://www.ruby-forum.com/.
Will Jessup
2006-Apr-16 02:33 UTC
[Rails] Re: New to rails - scaffold command , how to get related ite
Oh, as a future note - One of my category_id fields in the categories table was blank, this caused an error as well. -- Posted via http://www.ruby-forum.com/.
On Sun, 2006-04-16 at 04:15 +0200, Will Jessup wrote:> 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) >---- 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/tut_classes.html Craig
On Sun, 2006-04-16 at 04:15 +0200, Will Jessup wrote:> 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)---- 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