Hello there, I''m wondering how do they create functions on the fly? find_by_any_column_name ?? I want to do something similar with my models, but I don''t really know how to do it? user_current_nickname user_current_id user_current_role etc.? Regards, Jamal -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal, The find_by methods are illusions; they don''t actually exist. What you''re looking for is method_missing(name, args). When you attempt to send a message (call a method on) an instance of an object that doesn''t exist, method_missing is called instead. You can then decide behavior based on name and args. Hope this helps. Sean On 3/27/07, Jamal Soueidan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hello there, > > I''m wondering how do they create functions on the fly? > > find_by_any_column_name ?? > > I want to do something similar with my models, but I don''t really know > how to do it? > > user_current_nickname > user_current_id > user_current_role > > etc.? > > Regards, > Jamal > > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan wrote:> Hello there, > > I''m wondering how do they create functions on the fly? > > find_by_any_column_name ?? > > I want to do something similar with my models, but I don''t really know > how to do it? > > user_current_nickname > user_current_id > user_current_role > > etc.? > > Regards, > JamalI found out, you should use the method_missing method implemented in Ruby. DAMN I LOVE RUBY, it has every solution for you :D http://duncan.mac-vicar.com/blog/archives/18 -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Sean-Paul Rees wrote:> Jamal, > > The find_by methods are illusions; they don''t actually exist. > > What you''re looking for is method_missing(name, args). When you > attempt to send a message (call a method on) an instance of an object > that doesn''t exist, method_missing is called instead. You can then > decide behavior based on name and args. > > Hope this helps. > > Seanhttp://duncan.mac-vicar.com/blog/archives/18 Is described there :) but thanks anyway :D -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Hi,> The find_by methods are illusions; they don''t actually exist. > > When you attempt to send a message (call a method on) an instance of an object that doesn''t exist, method_missing is called instead. You can then decide behavior based on name and args. >Well.. the above is true, but it''s not the whole truth ;) method_missing is called when you invoke a non-existing method for an object. By checking the parameters you receive, you can then implement the behaviour you want. That would be perfect except for one thing. It slows down the calls to those "virtual" methods. fortunately, ruby is pretty dynamic and it allows to make nice things, like creating methods on the fly for a given object/class (I know classes are objects, but just to make it clear). In order to make the find_by_whatever efficient, the first time a find_by_something method is called method missing will be invoked, yes.. but what it does is creating on the fly a new method with that signature, so next time you use the method, it will be directly invoked, and the method_missing will be skipped entirely. cool, uh? regards, javier ramirez -- -------- Estamos de estreno... si necesitas llevar el control de tus gastos visita http://www.gastosgem.com !!Es gratis!! --~--~---------~--~----~------------~-------~--~----~ 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''m wondering how do they create functions on the fly?see: Under the hood: ActiveRecord::Base.find http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3 Alain --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alain Ravet wrote:>> I''m wondering how do they create functions on the fly? > > see: > Under the hood: ActiveRecord::Base.find > http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3 > > > AlainThank you very much, thats great help to :) -- 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?hl=en -~----------~----~----~----~------~----~------~--~---