Hey all, I''m looking at a piece of code developed by an experienced Rails developer: def create @user = User.authenticate!(params[:email], params[:password]) @deployment = Deployment.find_by_name(params[:deployment_name]) end In that third line, he calls a class method find_by_name on the Deployment class. So naturally I look at Deployment.rb where the class is initialized. But there is no find_by_name method. What I see is this: validates_presence_of :name def self.get(name) find_by_name!(name) end So why does this not return an undefined method error? There''s no setter or getter (attr_accessor) for find_by_name so it has no definition anywhere. Second point is find_by_name! is not declared anywhere either so we pass the name local variable into it, but it''s not defined anywhere. Anyone understand what''s going on here? Thanks for response. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> Hey all, > > I''m looking at a piece of code developed by an experienced Rails > developer: > > def create > @user = User.authenticate!(params[:email], params[:password]) > > @deployment = Deployment.find_by_name(params[:deployment_name]) > > end > > In that third line, he calls a class method find_by_name on the > Deployment class. So naturally I look at Deployment.rb where the class > is initialized. But there is no find_by_name method. What I see is this: > > validates_presence_of :name > > def self.get(name) > find_by_name!(name) > end > > > So why does this not return an undefined method error? There''s no setter > or getter (attr_accessor) for find_by_name so it has no definition > anywhere. Second point is find_by_name! is not declared anywhere either > so we pass the name local variable into it, but it''s not defined > anywhere. Anyone understand what''s going on here? Thanks for response.ActiveRecord (or Model, not sure which in Rails 3) has some dynamic finder methods. If "name" is a field for that model then "find_by_name" gets caught and converted into the appropriate method. See http://railscasts.com/episodes/2-dynamic-find-by-methods for more. Can''t find a quick link to the right section of the docs, but it''s there... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom wrote in post #977290:>> So why does this not return an undefined method error? There''s no setter >> or getter (attr_accessor) for find_by_name so it has no definition >> anywhere. Second point is find_by_name! is not declared anywhere either >> so we pass the name local variable into it, but it''s not defined >> anywhere. Anyone understand what''s going on here? Thanks for response. > > ActiveRecord (or Model, not sure which in Rails 3) has some dynamic > finder methods. If "name" is a field for that model then "find_by_name" > gets caught and converted into the appropriate method.As far as I can tell the dynamic finders are still implemented in ActiveRecord not ActiveModel. I''m not 100% sure of this however. Maybe because other implementers of ActiveModel may need to do their own custom behavior for this (if they want to behavior at all).> See http://railscasts.com/episodes/2-dynamic-find-by-methods for more. > Can''t find a quick link to the right section of the docs, but it''s > there...There''s good docs on this in the Rails guides here: http://guides.rubyonrails.org/active_record_querying.html#dynamic-finders -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.