# Overwrite active\_record find?

**URL:** <https://www.ruby-forum.com/t/overwrite-active-record-find/138962>\
**Category:** Rails\
**Created:** [May 29, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/overwrite-active-record-find/138962 "2008-05-29T14:17:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 29, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/overwrite-active-record-find/138962/1 "2008-05-29T14:17:51Z")

</div>

Hey!

i need to overwrite active record find to synchronize database access

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

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 29, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/overwrite-active-record-find/138962/2 "2008-05-29T14:26:44Z")

</div>

On 29 May 2008, at 13:17, Christian K. 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

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 29, 2008, 2:34pm UTC](https://www.ruby-forum.com/t/overwrite-active-record-find/138962/3 "2008-05-29T14:34:59Z")

</div>

Frederick C. wrote:

> 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

Problem is I’m using active\_record without rails on a application that  
includes a mini http server (built on top of webrick) and a background  
task. Both operate on the same sqlite db. Because of concurrency issues  
with sql I’m trying to synchronize access to the database.

Iven with super(\*args) i still get the 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’

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 29, 2008, 2:44pm UTC](https://www.ruby-forum.com/t/overwrite-active-record-find/138962/4 "2008-05-29T14:44:31Z")

</div>

On 29 May 2008, at 13:34, Christian K. wrote:

> > Fred
> 
> Problem is I’m using active\_record without rails on a application that  
> includes a mini http server (built on top of webrick) and a background  
> task. Both operate on the same sqlite db. Because of concurrency  
> issues  
> with sql I’m trying to synchronize access to the database.
> 
> Iven with super(\*args) i still get the error:

Should have been obvious - it looks like you’re not returning the  
right value (ie the result) from find\_by\_sql. Also bear in mind that  
find will call find\_by\_sql in some cases, so make sure whatever  
locking mechanism you’re using will support that. I’ve got a hint of a  
doubt that by background task you mean a separate process, in which  
case you’re out of luck (because each process would have its own lock).

Fred

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 29, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/overwrite-active-record-find/138962/5 "2008-05-29T15:40:52Z")

</div>

On 29 May 2008, at 14:05, Christian K. wrote:

> > lock).  
> > enough to syncronize just find\_by\_sql then?

Almost all. a find with an :include doesn’t in rails 2.0.2. This sort  
of thing could change in the future though.

Fred

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 29, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/overwrite-active-record-find/138962/6 "2008-05-29T15:05:08Z")

</div>

Frederick C. wrote:

> Should have been obvious - it looks like you’re not returning the  
> right value (ie the result) from find\_by\_sql. Also bear in mind that  
> find will call find\_by\_sql in some cases, so make sure whatever  
> locking mechanism you’re using will support that. I’ve got a hint of a  
> doubt that by background task you mean a separate process, in which  
> case you’re out of luck (because each process would have its own lock).

I switched the locking mechanism to thread.exclusive, because a mutex  
doesn’t work because find is calling find\_by\_sql. Added the returns.

def self.find(\*args)  
begin  
#@@semaphore.synchronize {  
Thread.exclusive {  
#puts “find”  
return super(args)

```
                }
            #}
        rescue
            puts "error: "+$!
           # @@semaphore.unlock
        end
    end

    def self.find_by_sql(sql)
       begin
            #@@semaphore.synchronize {
                Thread.exclusive {
                  #puts "find_by_sql"
                  return super(sql)
                }
            #}
        rescue
            puts "error: "+$!
           # @@semaphore.unlock
        end
    end

```

By background task is a second thread, not a seperate process, so the  
locking mechanism should work.

Is every find call using find\_by\_sql to retrieve data? Would it be  
enough to syncronize just find\_by\_sql then?

thx

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [May 30, 2008, 3:09pm UTC](https://www.ruby-forum.com/t/overwrite-active-record-find/138962/7 "2008-05-30T15:09:32Z")

</div>

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 C. \<
