# Object.send("collection \<\<", something) doesn't work?

**URL:** <https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125>\
**Category:** Rails\
**Created:** [October 30, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125 "2006-10-30T23:23:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [October 30, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125/1 "2006-10-30T23:23:53Z")

</div>

I’ve run into a problem with what I need to do. So let’s say I have an  
ActiveRecord object called Review which has\_many :computers. If I have  
an instance of Review and call review.computers \<\< computer that works  
fine. But if I do review.send('computers \<\< ', computer) that does not  
work and I get method\_missing. Is there a reason for this? Thanks for  
the help!

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [October 30, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125/2 "2006-10-30T23:36:35Z")

</div>

Wes G. wrote:

> Aaron W. wrote:
> 
> > I’ve run into a problem with what I need to do. So let’s say I have an  
> > ActiveRecord object called Review which has\_many :computers. If I have  
> > an instance of Review and call review.computers \<\< computer that works  
> > fine. But if I do review.send('computers \<\< ', computer) that does not  
> > work and I get method\_missing. Is there a reason for this? Thanks for  
> > the help!
> 
> Wouldn’t you have to do either:
> 
> review.computers.send(’\<\<’, computer)
> 
> OR
> 
> review.send(‘computers\<\<’, computer)
> 
> I don’t think there are any embedded spaces in method names, ever  
> (except when your database column has an embedded space for ActiveRecord  
> classes).
> 
> Wes

review.send(‘computers\<\<’, computer) fails regardless of the spaces.

review.computers.send(’\<\<’, computer) does work, but I still need to do  
the other way because I store the name of the collection in the database  
so I can do review.send("#{collection\_name}\<\<", something) depending on  
what collection I’m handling.

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [October 30, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125/3 "2006-10-30T23:30:35Z")

</div>

Aaron W. wrote:

> I’ve run into a problem with what I need to do. So let’s say I have an  
> ActiveRecord object called Review which has\_many :computers. If I have  
> an instance of Review and call review.computers \<\< computer that works  
> fine. But if I do review.send('computers \<\< ', computer) that does not  
> work and I get method\_missing. Is there a reason for this? Thanks for  
> the help!

Wouldn’t you have to do either:

review.computers.send(’\<\<’, computer)

OR

review.send(‘computers\<\<’, computer)

I don’t think there are any embedded spaces in method names, ever  
(except when your database column has an embedded space for ActiveRecord  
classes).

Wes

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [October 30, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125/4 "2006-10-30T23:48:23Z")

</div>

> You can do something like this:
> 
> eval(“review.#{collection\_name}.send(’\<\<’, something)”)
> 
> which will substitute collection name into a string that is then eval’ed
> 
> or you can do
> 
> intermediate\_collection = review.send(collection\_name)  
> intermediate\_collection \<\< something
> 
> Wes

Thanks for the help Wes! review.send(collection\_name) \<\< something works  
just fine.

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [October 30, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125/5 "2006-10-30T23:50:53Z")

</div>

Aaron W. wrote:

> > You can do something like this:
> > 
> > eval(“review.#{collection\_name}.send(’\<\<’, something)”)
> > 
> > which will substitute collection name into a string that is then eval’ed
> > 
> > or you can do
> > 
> > intermediate\_collection = review.send(collection\_name)  
> > intermediate\_collection \<\< something
> > 
> > Wes
> 
> Thanks for the help Wes! review.send(collection\_name) \<\< something works  
> just fine.

Even better. Ain’t Ruby purdy :)?

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [October 30, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125/6 "2006-10-30T23:41:59Z")

</div>

Aaron W. wrote:

> Wes G. wrote:
> 
> > Aaron W. wrote:
> > 
> > > I’ve run into a problem with what I need to do. So let’s say I have an  
> > > ActiveRecord object called Review which has\_many :computers. If I have  
> > > an instance of Review and call review.computers \<\< computer that works  
> > > fine. But if I do review.send('computers \<\< ', computer) that does not  
> > > work and I get method\_missing. Is there a reason for this? Thanks for  
> > > the help!
> > 
> > Wouldn’t you have to do either:
> > 
> > review.computers.send(’\<\<’, computer)
> > 
> > OR
> > 
> > review.send(‘computers\<\<’, computer)
> > 
> > I don’t think there are any embedded spaces in method names, ever  
> > (except when your database column has an embedded space for ActiveRecord  
> > classes).
> > 
> > Wes
> 
> review.send(‘computers\<\<’, computer) fails regardless of the spaces.
> 
> review.computers.send(’\<\<’, computer) does work, but I still need to do  
> the other way because I store the name of the collection in the database  
> so I can do review.send("#{collection\_name}\<\<", something) depending on  
> what collection I’m handling.

You can do something like this:

eval(“review.#{collection\_name}.send(’\<\<’, something)”)

which will substitute collection name into a string that is then eval’ed

or you can do

intermediate\_collection = review.send(collection\_name)  
intermediate\_collection \<\< something

Wes

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [November 7, 2006, 10:47am UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125/7 "2006-11-07T10:47:08Z")

</div>

Aaron W. wrote:

> review.send(‘computers\<\<’, computer) fails regardless of the spaces.

That’s because there is no method named “computers\<\<” on the review  
object. There is, however, a method named “\<\<” on the object returned  
by the call to the review.computers method.

If your collection is dynamically named, then you can use send twice to  
get the appropriate collection:  
review.send( collection\_name ).send( ‘\<\<’, computer )

Hope that helps,  
Gavin

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [October 30, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/object-send-collection-something-doesnt-work/74125/8 "2006-10-30T23:58:46Z")

</div>

Wes G. wrote:

> Even better. Ain’t Ruby purdy :)?

Gorgeous! 🙂 Thanks again
