# Association Extensions

**URL:** <https://www.ruby-forum.com/t/association-extensions/97175>\
**Category:** Rails\
**Created:** [July 5, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/association-extensions/97175 "2006-07-05T14:10:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [July 5, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/association-extensions/97175/1 "2006-07-05T14:10:22Z")

</div>

Hi,  
I’m using an assocation extension as follows:

module RequestFinder  
def open  
find(:all, :conditions =\> “buyer\_requests.closing\_date \>  
‘#{Date.today}’”)  
end

def closed  
find(:all, :conditions =\> “buyer\_requests.closing\_date \<=  
‘#{Date.today}’”)  
end  
end

with the following ActiveRecord

class User \< ActiveRecord::Base  
has\_many :my\_requests, :class\_name =\> “BuyerRequest”, :extend =\>  
RequestFinder  
end

In my tests I have a test to check the number of requests.

def test\_number\_of \_open\_requests  
john = users(:john)  
openrequests = john.my\_requests.open  
assert openrequests.count == 1  
end  
Although this returns an array with the correct values my problem is  
that the count method is undefined. Does anyone know if thats expected  
behaviour, according the Rails recipes books each of the assocation  
proxy methods should be available on the returned object i.e. find,  
create, count etc…

Thanks in advance,

John

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [July 5, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/association-extensions/97175/2 "2006-07-05T14:16:47Z")

</div>

On 7/5/06, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> find(:all, :conditions => "buyer_requests.closing_date <=
> 
> ```
> 
> according the Rails recipes books each of the assocation proxy methods  
> should be available on the returned object i.e. find, create, count etc…

john.my\_requests is an association proxy (with the proxy methods  
defined on it, including those in your extension).  
john.my\_requests.open is just a call to a normal find method, so  
returns a bog standard array.

Tom

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [July 5, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/association-extensions/97175/3 "2006-07-05T14:26:24Z")

</div>

Tom,  
With your clarification I’ve reread the rails recipes statement.  
“These association proxies have access to all the same methods that  
would  
normally be defined on the associations, such as find( ), count( ), and  
create( ).”

john.my\_requests has these methods so this is correct.

Thanks,  
John

----- Original Message -----  
From: “Tom W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, July 05, 2006 1:15 PM  
Subject: Re: [Rails] Association Extensions
