Was browsing the source for Typo. http://typo.leetsoft.com/trac.cgi/file/tags/release_2_0_6/app/models/article.rb Why use def self.search(query) and not search(query)? Thanks, Joe
On 5/18/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Was browsing the source for Typo. > http://typo.leetsoft.com/trac.cgi/file/tags/release_2_0_6/app/models/article.rb > > Why use def self.search(query) and not search(query)?Another question: See http://typo.leetsoft.com/trac.cgi/file/tags/release_2_0_6/app/controllers/articles_controller.rb def index @pages = Paginator.new self, Article.count, 10, @params[''page''] @articles = Article.find(:all, :conditions => ''published != 0'', :order => ''articles.created_at DESC'', :limit => 10, :offset => @pages.current.offset) end Wouldn''t it make sense to make a method for Article called #recently_posted_articles that returned an array of Articles meeting the criteria specified in the controller above? I suppose it would have to be a class method of Article, right? Any problems with that? Or is that not good design? In my first Rails app, my controllers were getting huge, while there weren''t much in my models, so I''m trying to figure out ways to put more code in the model. Thanks, Joe
On Thursday, May 19, 2005, 7:40:01 AM, Joe wrote:> Was browsing the source for Typo. > http://typo.leetsoft.com/trac.cgi/file/tags/release_2_0_6/app/models/article.rb> Why use def self.search(query) and not search(query)?class X def self.foo() 14 end def bar() 5 end end X.foo # -> 14 X.bar # -> no such method x = X.new x.foo # no such method x.bar # -> 5 Gavin
Joe Van Dyk wrote:> def index > @pages = Paginator.new self, Article.count, 10, @params[''page''] > @articles = Article.find(:all, :conditions => ''published != 0'', > :order => ''articles.created_at DESC'', > :limit => 10, :offset => > @pages.current.offset) > end > > Wouldn''t it make sense to make a method for Article called > #recently_posted_articles that returned an array of Articles meeting > the criteria specified in the controller above? > > I suppose it would have to be a class method of Article, right? Any > problems with that? > > Or is that not good design? In my first Rails app, my controllers > were getting huge, while there weren''t much in my models, so I''m > trying to figure out ways to put more code in the model.Yes, your instincts are right. You should definately push that finder to the model as a class method. Leave the controllers as thin as possible, only as glue code between the user interface and models. rgds Dema http://dema.ruby.com.br/
Thank you Gavin, I''m not ask but I really want to know it. So I can change class attributes only on methods widout *self.*, I''m right? Can you send a example like this, showing how create attributes and change in a method?? Pedro Gavin Sinclair escreveu:>On Thursday, May 19, 2005, 7:40:01 AM, Joe wrote: > > > >>Was browsing the source for Typo. >>http://typo.leetsoft.com/trac.cgi/file/tags/release_2_0_6/app/models/article.rb >> >> > > > >>Why use def self.search(query) and not search(query)? >> >> > > class X > def self.foo() 14 end > def bar() 5 end > end > > X.foo # -> 14 > X.bar # -> no such method > > x = X.new > x.foo # no such method > x.bar # -> 5 > >Gavin > > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Pedro C. Valentini pedro-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org +55 (21) 8708-8035
On Wed, 18 May 2005, Pedro Valentini wrote:> So I can change class attributes only on methods widout *self.*, I''m > right?http://www.rubycentral.com/book/ http://www.pragmaticprogrammer.com/titles/ruby/index.html --Steve
--------- Forwarded message ---------- From: Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> Date: May 18, 2005 3:55 PM Subject: Re: [Rails] Why use self.method To: Gavin Sinclair <gsinclair-81uBx+iSpXA0n/F98K4Iww@public.gmane.org> On 5/18/05, Gavin Sinclair <gsinclair-81uBx+iSpXA0n/F98K4Iww@public.gmane.org> wrote:> On Thursday, May 19, 2005, 7:40:01 AM, Joe wrote: > > > Was browsing the source for Typo. > > http://typo.leetsoft.com/trac.cgi/file/tags/release_2_0_6/app/models/article.rb > > > Why use def self.search(query) and not search(query)? > > class X > def self.foo() 14 end > def bar() 5 end > end > > X.foo # -> 14 > X.bar # -> no such method > > x = X.new > x.foo # no such method > x.bar # -> 5Ah, it''s a class method? So, would you use that for the original post''s question?
On Thursday, May 19, 2005, 8:56:02 AM, Joe wrote:>> > Was browsing the source for Typo. >> > http://typo.leetsoft.com/trac.cgi/file/tags/release_2_0_6/app/models/article.rb >> > >> > Why use def self.search(query) and not search(query)? >> >> class X >> def self.foo() 14 end >> def bar() 5 end >> end >> >> X.foo # -> 14 >> X.bar # -> no such method >> >> x = X.new >> x.foo # no such method >> x.bar # -> 5> Ah, it''s a class method?Correct.> So, would you use that for the original post''s question?The source code in article.rb gives rise to the following usage: Article.search("foo bar quux") Instead of: a = Article.find(1) a.search("foo bar quux") Searching has nothing to do with a _particular_ article (a) but the general model Article. So it''s a class method. Cheers, Gavin