Re: Overwrite active_record find?

Russell Norris
Fri, 30 May 2008 12:52:28 -0000

FWIW you can just use super [with no args] if it''s going to use the
same
arguments passed to the method. This works even if you modify the arguments
like:

  def label(object_name, method, text = nil, options = {})
    text ||= method.to_s[].titleize
    super
  end

RSL

On Thu, May 29, 2008 at 8:26 AM, Frederick Cheung <
frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

>
>
> On 29 May 2008, at 13:17, Christian Kerth wrote:
>
> >
> > Hey!
> >
> > i need to overwrite active record find to synchronize database access
> >
> You need to say super(*args). alias_method_chain is usually a more
> prudent choice for this sort of stuff (and i assume you know that this
> won''t accomplish anything if you''re using a pack of
mongrels or
> whatever - I suppose it would work if you had a single multithreaded
> script, but I''m still not sure what the point would be)
>
> Fred
> > i''ve tried the following:
> >
> > def self.find(*args)
> >           begin
> >                @@semaphore.synchronize {
> >
> >
> >                        super(args)
> >
> >
> >                }
> >            rescue
> >                puts "error: "+$!
> >                @@semaphore.unlock
> >            end
> >        end
> >
> >        def self.find_by_sql(sql)
> >
> >           begin
> >                @@semaphore.synchronize {
> >
> >
> >                      super(sql)
> >
> >
> >                }
> >            rescue
> >                puts "error: "+$!
> >                @@semaphore.unlock
> >            end
> >        end
> >
> > I always get an error:
> >
> > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
> > base.rb:1227:in
> > `find_initial'': undefined method `first'' for
nil:NilClass
> > (NoMethodError)
> >        from
> > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
> > base.rb:1514:in
> > `send''
> >        from
> > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
> > base.rb:1514:in
> > `find_by_requesttype_and_requeststatus''
> >
> >
> >
> > Obviously the dynamic finder methos like
> > find_by_equesttype_and_requeststatus doesn''t work.
> >
> > Any clues to fix this?
> >
> > Thx
> > --
> > Posted via http://www.ruby-forum.com/.
> >
> > >