Hello, there is a method ActiveRecord::Base#exists? . I would like to override this method in a class ActiveRecord. Could anyone tell me how to do it? TIA -- Posted via http://www.ruby-forum.com/.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Firstname Secondname wrote:> Hello, > there is a method ActiveRecord::Base#exists? . I would like to override > this method in a class ActiveRecord. > > Could anyone tell me how to do it?Before you override this, why do you want to override this? Perhaps there is a better way to achieve what you want rather then changing the behavior of exists? Zach -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFE3f4fMyx0fW1d8G0RAmxZAJ93g65XkKWLonznEjuBy9aDg4185QCdFMJL ml5xsC6RWOxxik5Fi4rskvo=y3IH -----END PGP SIGNATURE-----
Hello ''firstname'', Well, since Rails is, at its heart, jst ''good old ruby'', jst override the class method the same way you would in ruby. Eg; >> print Booking.exists?(153153) true=> nil >> class Booking >> def Booking.exists?(str) >> puts " over-ridden method " >> super(str) >> end >> end => nil >> print Booking.exists?(153153) over-ridden method true=> nil Of course, if your over-ridding a class that lots of other classes inherit from (eg; ActiveRecord::Base) you may jst want to actually over-ride the method in the class where you need it. I strongly doubt you need to over-ride the -all- the descendants of Base.. but.. your choice ;) Regards Stef Firstname Secondname wrote:> Hello, > there is a method ActiveRecord::Base#exists? . I would like to override > this method in a class ActiveRecord. > > Could anyone tell me how to do it? > > TIA > >
Firstname Secondname wrote:> Hello, > there is a method ActiveRecord::Base#exists? . I would like to override > this method in a class ActiveRecord. > > Could anyone tell me how to do it? > > TIAI can easily extend ApplicationController. How do I extend ActiveRecord? -- Posted via http://www.ruby-forum.com/.
class ActiveRecord::Base # your code here end -Jonathan. On 8/13/06, Firstname Secondname <mzilenas@gmail.com> wrote:> > Firstname Secondname wrote: > > Hello, > > there is a method ActiveRecord::Base#exists? . I would like to override > > this method in a class ActiveRecord. > > > > Could anyone tell me how to do it? > > > > TIA > > I can easily extend ApplicationController. How do I extend ActiveRecord? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060814/378bfbb1/attachment.html
Firstname Secondname wrote:> Firstname Secondname wrote: >> Hello, >> there is a method ActiveRecord::Base#exists? . I would like to override >> this method in a class ActiveRecord. >> >> Could anyone tell me how to do it? >> >> TIA > > I can easily extend ApplicationController. How do I extend ActiveRecord?Could you provide skeleton file (.rb) and tell me where to put it so rails could pick it up and override method exists? in ActiveRecord::Base . SELECT COUNT(*) FROM table WHERE id = ? is slower than SELECT COUNT(*) FROM table WHERE id = ? LIMIT 1 I would like to use my own solution with exists? method. -- Posted via http://www.ruby-forum.com/.
The exists? method already applies the LIMIT 1 by using find(:first, ...). Maybe I am misunderstanding your problem? -Gregory Gross On 8/15/06, Firstname Secondname <mzilenas@gmail.com> wrote:> Firstname Secondname wrote: > > Firstname Secondname wrote: > >> Hello, > >> there is a method ActiveRecord::Base#exists? . I would like to override > >> this method in a class ActiveRecord. > >> > >> Could anyone tell me how to do it? > >> > >> TIA > > > > I can easily extend ApplicationController. How do I extend ActiveRecord? > > Could you provide skeleton file (.rb) and tell me where to put it so > rails could pick it up and override method exists? in ActiveRecord::Base > . > > SELECT COUNT(*) FROM table WHERE id = ? is slower than SELECT COUNT(*) > FROM table WHERE id = ? LIMIT 1 > > I would like to use my own solution with exists? method. > > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Firstname Secondname
2006-Aug-15 18:15 UTC
[Rails] Re: Re: Override method in ActiveRecord
Greg Gross wrote:> The exists? method already applies the LIMIT 1 by using find(:first, > ...). > > Maybe I am misunderstanding your problem? > > -Gregory GrossYou are right. The problem is solved. Thank you -- Posted via http://www.ruby-forum.com/.