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>
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
···
def self.find(*args)
print "doing find\n";
kellys_find(args)
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?
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.
···
def find(*arg)
puts "doing find"
# ^^ puts is is better here instead of print
kellys_find(*args)
# ^ use asterisk here
end
end
end
def self.find(*args)
print "doing find\n";
kellys_find(args)
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?