Hi, is it possible to override Active Record find() in a class that
inherits Active Record?
eg.
def find
super
end
I’ve tried it and it doesn’t seem to work!
Hi, is it possible to override Active Record find() in a class that
inherits Active Record?
eg.
def find
super
end
I’ve tried it and it doesn’t seem to work!
It can be overridden, but it depends on your requirement. Do you just
want to process a result_set returned from the super? Or do you want
to pass additional options to the super? Or do you want to rewrite it
completely?
It is a bit difficult say since the requirement is unclear.
Long
Find is a class method. Your example below creates an instance method
called find. To override the AR find class method do something like
this:
def ClassName.find(*args)
super(*args)
end
Hi Lee –
On 8-Sep-06, at 3:58 PM, Lee wrote:
Hi, is it possible to override Active Record find() in a class that
inherits Active Record?def find
super
endI’ve tried it and it doesn’t seem to work!
Yes, it’s possible… the reason it’s not working for you is because
find is a class method, but you’ve written an instance method. Try:
def self.find
end
instead.
/Jeff
On 8-Sep-06, at 6:04 PM, Aaron B. wrote:
def ClassName.find(*args)
super(*args)
end
Actually, super’s default behaviour is to slurp up all the args that
came into the method in the first place, so the argument is
superfluous. See this post for an example of overriding find to set a
default order for a model.
http://groups.google.com/group/rubyonrails-talk/msg/5ef476cfdd0b3797
/Jeff
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs