Override method in ActiveRecord

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?

TIA

I can easily extend ApplicationController. How do I extend ActiveRecord?

-----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 :wink:

Regards
Stef

class ActiveRecord::Base

your code here

end

-Jonathan.

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.

The exists? method already applies the LIMIT 1 by using find(:first,
…).

Maybe I am misunderstanding your problem?

-Gregory Gross

Greg Gross wrote:

The exists? method already applies the LIMIT 1 by using find(:first,
…).

Maybe I am misunderstanding your problem?

-Gregory Gross

You are right.
The problem is solved.

Thank you

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs