# Habtm \> join()

**URL:** <https://www.ruby-forum.com/t/habtm-join/84791>\
**Category:** Rails\
**Created:** [March 21, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/habtm-join/84791 "2006-03-21T18:12:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f04885/32.png) [@zaq](https://www.ruby-forum.com/u/zaq)\
**Post date:** [March 21, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/habtm-join/84791/1 "2006-03-21T18:12:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zaq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f04885/32.png) [@zaq](https://www.ruby-forum.com/u/zaq)\
**Post date:** [March 21, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/habtm-join/84791/2 "2006-03-21T18:18:49Z")

</div>

I need a persistent message queue, similar to JMS. Is there a  
Rails/Ruby equivalent?

---

<div class="post-metadata">

**Author:** ![zaq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f04885/32.png) [@zaq](https://www.ruby-forum.com/u/zaq)\
**Post date:** [March 21, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/habtm-join/84791/3 "2006-03-21T18:48:59Z")

</div>

On 3/21/06, Jeff de Vries [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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://activemq.codehaus.org/)  
[http://stomp.codehaus.org/](http://stomp.codehaus.org/)  
[http://rubyforge.org/projects/stomp/](http://rubyforge.org/projects/stomp/)

You can use it simultaneously from various languages, including Ruby and  
Java.

---

<div class="post-metadata">

**Author:** ![zaq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f04885/32.png) [@zaq](https://www.ruby-forum.com/u/zaq)\
**Post date:** [March 21, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/habtm-join/84791/4 "2006-03-21T18:42:15Z")

</div>

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](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![zaq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/f04885/32.png) [@zaq](https://www.ruby-forum.com/u/zaq)\
**Post date:** [March 22, 2006, 3:18am UTC](https://www.ruby-forum.com/t/habtm-join/84791/5 "2006-03-22T03:18:23Z")

</div>

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
