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

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!

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.

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

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.

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 :)?

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

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

Wes G. wrote:

Even better. Ain’t Ruby purdy :)?

Gorgeous! :slight_smile: Thanks again

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs