Lance Squire
2006-Jun-12 17:52 UTC
[Rails] Getting 1 from children of has_many, Noob question...
The ''Agile Web'' book says that you can iterate over a list of children as so: order.line-items.each do |li| etc... This is fine when you need then all consecuitvely, but I need to pull the first one for the top of the page and still list all at the bottom. So, the question is: How do I reference the first item (child) directly? Thanks Lance F. Squire -- Posted via http://www.ruby-forum.com/.
Chris T
2006-Jun-12 18:10 UTC
[Rails] Getting 1 from children of has_many, Noob question...
Not tested, but something like... order.line-items.first # for the first one (order.line-items - order.line-items.first).each do # for the remaining ones Tho'' would be better to define the first one and remaining ones in the controller; @first_line-item = order.line-items.first @remaining_line-items = order.line-items - order.line-items.first HTH Lance Squire wrote:> The ''Agile Web'' book says that you can iterate over a list of children > as so: > > order.line-items.each do |li| > etc... > > This is fine when you need then all consecuitvely, but I need to pull > the first one for the top of the page and still list all at the bottom. > > So, the question is: > > How do I reference the first item (child) directly? > > Thanks > > Lance F. Squire > >
Lance Squire
2006-Jun-12 19:48 UTC
[Rails] Re: Getting 1 from children of has_many, Noob question...
Chris T wrote:> Not tested, but something like... > > order.line-items.first # for the first one > >Thanks! This worked well. Had to change it to something like: order.line-items.first.unit_price to get what I wanted. Needed compleate list at bottom, so extra code not need but good for future reference. Thanks, Lance -- Posted via http://www.ruby-forum.com/.