Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help me override ActiveRecord find?

1 view
Skip to first unread message

Kelly Felkins

unread,
Aug 8, 2005, 12:37:38 PM8/8/05
to
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"
def self.find(*args)
print "doing find\n";
kellys_find(args)
end
end

and I get this:

kellyf@sb61g2:/var/www/rl-dev$ script/console
Loading development environment.
irb(main):001:0> t1 = 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/dependencies.rb:189:in
`load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dependencies.rb:189:in
`load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dependencies.rb:38:in
`require_or_load'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dependencies.rb:21:in
`depend_on'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dependencies.rb:167:in
`require_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dependencies.rb:167:in
`require_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dependencies.rb:180:in
`const_missing'
from (irb):1
irb(main):002:0>

Any suggestions?

Gennady Bystritksy

unread,
Aug 8, 2005, 12:51:01 PM8/8/05
to
Kelly Felkins wrote:
> 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

Gennady Bystritksy

unread,
Aug 8, 2005, 12:54:16 PM8/8/05
to
Gennady Bystritksy wrote:
> Kelly Felkins wrote:
>
>> 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"

Oops, "alias_method" here, of course.

0 new messages