Forum: Ruby on Rails habtm > join()

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.
Af656cca6a3806277fe19a0c606ddab7?d=identicon&s=25 Zac Elston (zaq)
on 2006-03-21 18:12
I'm trying to pull ou the 'name' field for each of my associated rows
and pass it as a string of names.  I'm missing something obvious here
but I thought  @bag.packages.name.join(", ") would work.

do I have to loop through @bag.packages?  that seems like the long way.

thanks

-zac
C7407d9ae305656e88bc7935d4af9cbd?d=identicon&s=25 Jeff de Vries (Guest)
on 2006-03-21 18:18
(Received via mailing list)
I need a persistent message queue, similar to JMS.  Is there a
Rails/Ruby equivalent?
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-03-21 18:42
zac elston wrote:
> I'm trying to pull ou the 'name' field for each of my associated rows
> and pass it as a string of names.  I'm missing something obvious here
> but I thought  @bag.packages.name.join(", ") would work.
>
> do I have to loop through @bag.packages?  that seems like the long way.

You have to loop or do a custom query. Use the map message to do the
loop:

@bag.packages.map { |p| p.name }.join(", ")

In Edge/1.1 you can type that as @bag.packages.map(&:name).join(", ")

If you don't want to fetch all the package data but only get the list of
names, you'll have to construct a custom query using the :select option
on find(). This is probably a good use for an association extension.

--josh
http://blog.hasmanythrough.com
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-03-21 18:48
(Received via mailing list)
On 3/21/06, Jeff de Vries <jdevries@pfrog.com> wrote:
> I need a persistent message queue, similar to JMS.  Is there a
> Rails/Ruby equivalent?
>

Check out ActiveMQ and Stomp:
http://activemq.codehaus.org/
http://stomp.codehaus.org/
http://rubyforge.org/projects/stomp/

You can use it simultaneously from various languages, including Ruby and
Java.
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-22 03:18
(Received via mailing list)
zac elston wrote:
> I'm trying to pull ou the 'name' field for each of my associated rows
> and pass it as a string of names.  I'm missing something obvious here
> but I thought  @bag.packages.name.join(", ") would work.
>
> do I have to loop through @bag.packages?  that seems like the long way.

You can't ask the packages collection for the names of all the packages
it contains. You do have to iterate over the collection, but it doesn't
involve a lot of code.

try

   @bag.packages.collect {|p| p.name}.join(', ')

regards

   Justin
This topic is locked and can not be replied to.