Aaron Wieberg
2006-Oct-30 22:23 UTC
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Wes Gamble
2006-Oct-30 22:30 UTC
Re: object.send("collection <<", something) doesn''t work?
Aaron Wieberg 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Aaron Wieberg
2006-Oct-30 22:36 UTC
Re: object.send("collection <<", something) doesn''t work?
Wes Gamble wrote:> Aaron Wieberg 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). > > Wesreview.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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Wes Gamble
2006-Oct-30 22:41 UTC
Re: object.send("collection <<", something) doesn''t work?
Aaron Wieberg wrote:> Wes Gamble wrote: >> Aaron Wieberg 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Aaron Wieberg
2006-Oct-30 22:48 UTC
Re: object.send("collection <<", something) doesn''t work?
> 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 > > WesThanks for the help Wes! review.send(collection_name) << something works just fine. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Aaron Wieberg 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Wes Gamble
2006-Oct-30 22:50 UTC
Re: object.send("collection <<", something) doesn''t work?
Aaron Wieberg 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 :)? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Aaron Wieberg
2006-Oct-30 22:58 UTC
Re: object.send("collection <<", something) doesn''t work?
Wes Gamble wrote:> Even better. Ain''t Ruby purdy :)?Gorgeous! :) Thanks again -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---