I am trying to call a class method from an instance, e.g. @item = @item.class.find(params[:id]) or @item = @item.type.find(params[:id]) @Item can reference objects form different classes. With either of the above examples I get: undefined method `find'' for NilClass:Class Any help? --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
I don''t know that it is the best way, but you can do this with eval. Eval will take a string and interpret it as ruby code. So you would do something like: eval( @item.class + ''.find(params[:id])'' ) So this should take your class name, append the find clause and then run the result. On Feb 5, 3:19 pm, "B. Lars" <lbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am trying to call a class method from an instance, > > e.g. @item = @item.class.find(params[:id]) or > @item = @item.type.find(params[:id]) > > @Item can reference objects form different classes. > > With either of the above examples I get: > undefined method `find'' for NilClass:Class > > Any help?--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Ok I tried eval( @item.class.to_s + ''.find(params[:id])'' ) and i still get the error undefined method `find'' for NilClass:Class error. On Feb 5, 2:26 pm, "monki" <gee...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I don''t know that it is the best way, but you can do this with eval. > Eval will take a string and interpret it as ruby code. > > So you would do something like: > > eval( @item.class + ''.find(params[:id])'' ) > > So this should take your class name, append the find clause and then > run the result. > > On Feb 5, 3:19 pm, "B. Lars" <lbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am trying to call a class method from an instance, > > > e.g. @item = @item.class.find(params[:id]) or > > @item = @item.type.find(params[:id]) > > > @Item can reference objects form different classes. > > > With either of the above examples I get: > > undefined method `find'' for NilClass:Class > > > Any help?--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
stupid mistake by me I think I know the problem, i''ll post again when i''m sure. On Feb 5, 2:26 pm, "monki" <gee...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I don''t know that it is the best way, but you can do this with eval. > Eval will take a string and interpret it as ruby code. > > So you would do something like: > > eval( @item.class + ''.find(params[:id])'' ) > > So this should take your class name, append the find clause and then > run the result. > > On Feb 5, 3:19 pm, "B. Lars" <lbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am trying to call a class method from an instance, > > > e.g. @item = @item.class.find(params[:id]) or > > @item = @item.type.find(params[:id]) > > > @Item can reference objects form different classes. > > > With either of the above examples I get: > > undefined method `find'' for NilClass:Class > > > Any help?--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
I got it working with, @item = @items.pop.type.find(params[:id]) I just wasn''t initializing@item with anything. Thanks monki for the eval hint though. I wasn''t aware of the eval usage. On Feb 5, 2:39 pm, "B. Lars" <lbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> stupid mistake by me I think I know the problem, i''ll post again when > i''m sure. > > On Feb 5, 2:26 pm, "monki" <gee...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I don''t know that it is the best way, but you can do this with eval. > > Eval will take a string and interpret it as ruby code. > > > So you would do something like: > > > eval( @item.class + ''.find(params[:id])'' ) > > > So this should take your class name, append the find clause and then > > run the result. > > > On Feb 5, 3:19 pm, "B. Lars" <lbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I am trying to call a class method from an instance, > > > > e.g. @item = @item.class.find(params[:id]) or > > > @item = @item.type.find(params[:id]) > > > > @Item can reference objects form different classes. > > > > With either of the above examples I get: > > > undefined method `find'' for NilClass:Class > > > > Any help?--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Good to know, I guess I overlooked the simple answer. Your way is better, as eval will blindly do whatever you tell it (which can be dangerous). --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Your original method was better (IMHO) -- @item.class.find(params[:id]) -- as type is deprecated and can sometimes behave unexpectedly as it''s used as the key in STI models. Also wanted to check you knew that @items.pop is a destructive method, i.e. the item is removed from @items. I would wonder if @items.last.class.find(params[:id]) is perhaps a bit cleaner. You''ve prob figured now that undefined method `find'' for NilClass:Class was happening because @item was nil (instance variables are nil when they are undefined, unlike local variables -- try p @item and p item in the console), and the class of nil is NilClass, which obviously doesn''t have a find method. HTH Chris B. Lars wrote:> I got it working with, > @item = @items.pop.type.find(params[:id]) > > I just wasn''t initializing@item with anything. > > Thanks monki for the eval hint though. I wasn''t aware of the eval > usage. > > On Feb 5, 2:39 pm, "B. Lars" <lbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> stupid mistake by me I think I know the problem, i''ll post again when >> i''m sure. >> >> On Feb 5, 2:26 pm, "monki" <gee...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >>> I don''t know that it is the best way, but you can do this with eval. >>> Eval will take a string and interpret it as ruby code. >>> >>> So you would do something like: >>> >>> eval( @item.class + ''.find(params[:id])'' ) >>> >>> So this should take your class name, append the find clause and then >>> run the result. >>> >>> On Feb 5, 3:19 pm, "B. Lars" <lbr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> >>>> I am trying to call a class method from an instance, >>>> >>>> e.g. @item = @item.class.find(params[:id]) or >>>> @item = @item.type.find(params[:id]) >>>> >>>> @Item can reference objects form different classes. >>>> >>>> With either of the above examples I get: >>>> undefined method `find'' for NilClass:Class >>>> >>>> Any help? >>>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---