help me override ActiveRecord find?

K

Kelly Felkins

------=_Part_6954_30644496.1123519056058
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I would like to add some things to the find method of activerecord, but I=
=20
can't figure out how to do this.

I've tried various things. Here is one example:

class Thing < ActiveRecord::Base
alias_method "kellys_find", "find"
def self.find(*args)
print "doing find\n";
kellys_find(args)
end
end
=20
and I get this:

kellyf@sb61g2:/var/www/rl-dev$ script/console
Loading development environment.
irb(main):001:0> t1 =3D Thing.find(2)
NameError: undefined method `find' for class `Thing'
from ./script/../config/..//app/models/thing.rb:12:in `alias_method'
from ./script/../config/..//app/models/thing.rb:12
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dep=
endencies.rb:189:in
`load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dep=
endencies.rb:189:in
`load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dep=
endencies.rb:38:in
`require_or_load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dep=
endencies.rb:21:in
`depend_on'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dep=
endencies.rb:167:in
`require_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dep=
endencies.rb:167:in
`require_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dep=
endencies.rb:180:in
`const_missing'
from (irb):1
irb(main):002:0>=20
=20
Any suggestions?

------=_Part_6954_30644496.1123519056058--
 
G

Gennady Bystritksy

Kelly said:
I would like to add some things to the find method of activerecord, but I
can't figure out how to do this.

I've tried various things. Here is one example:

class Thing < ActiveRecord::Base
alias_method "kellys_find", "find"

Here you try to create an alias "kellys_find" for instance method "find"
(no "find" method for instances, hence the error).

Try something like this:

class Thing < ActiveRecord::Base
class << self
alias "kellys_find", "find"
def find(*arg)
puts "doing find"
# ^^ puts is is better here instead of print
kellys_find(*args)
# ^ use asterisk here
end
end
end
 
G

Gennady Bystritksy

Gennady said:
Here you try to create an alias "kellys_find" for instance method "find"
(no "find" method for instances, hence the error).

Try something like this:

class Thing < ActiveRecord::Base
class << self
alias "kellys_find", "find"

Oops, "alias_method" here, of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top