# Altering the accessor of an association collection

**URL:** <https://www.ruby-forum.com/t/altering-the-accessor-of-an-association-collection/51529>\
**Category:** Rails\
**Created:** [January 2, 2006, 1:24pm UTC](https://www.ruby-forum.com/t/altering-the-accessor-of-an-association-collection/51529 "2006-01-02T13:24:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [January 2, 2006, 1:24pm UTC](https://www.ruby-forum.com/t/altering-the-accessor-of-an-association-collection/51529/1 "2006-01-02T13:24:47Z")

</div>

I want to be able to do something like this (for example):

class Person \< ActiveRecord::Base  
has\_many :friends

def friends(living\_in = :uk)  
# filter based on parameters here  
end  
end

Is there any way I can redefine the accessor for an association whilst  
still being able to get the values? Basically, I’m looking for an  
equivilent of read\_attribute() for associations.

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [January 2, 2006, 1:45pm UTC](https://www.ruby-forum.com/t/altering-the-accessor-of-an-association-collection/51529/2 "2006-01-02T13:45:50Z")

</div>

Actually don’t worry about this, I changed my code so I don’t need it.

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [January 2, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/altering-the-accessor-of-an-association-collection/51529/3 "2006-01-02T13:46:33Z")

</div>

On 2.1.2006, at 14.24, Jon L. wrote:

> I want to be able to do something like this (for example):
> 
> class Person \< ActiveRecord::Base  
> has\_many :friends
> 
> def friends(living\_in = :uk)  
> # filter based on parameters here  
> end

You can use the automatic Active Record methods for this, too:

jake = Person.find :first  
jake.friends.find\_all\_by\_living\_in(“uk”)

If you think it’s worth it, you can also write your own friends  
method. I can’t really see a reason for that, though. Maybe I  
misunderstood something.

//jarkko

---

<div class="post-metadata">

**Author:** ![turnip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@turnip](https://www.ruby-forum.com/u/turnip)\
**Post date:** [January 2, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/altering-the-accessor-of-an-association-collection/51529/4 "2006-01-02T17:43:02Z")

</div>

Jarkko L. wrote:

> On 2.1.2006, at 14.24, Jon L. wrote:
> 
> > I want to be able to do something like this (for example):
> > 
> > class Person \< ActiveRecord::Base  
> > has\_many :friends
> > 
> > def friends(living\_in = :uk)  
> > # filter based on parameters here  
> > end
> 
> You can use the automatic Active Record methods for this, too:
> 
> jake = Person.find :first  
> jake.friends.find\_all\_by\_living\_in(“uk”)
> 
> If you think it’s worth it, you can also write your own friends  
> method. I can’t really see a reason for that, though. Maybe I  
> misunderstood something.

Perhaps my example wasn’t the best – it’s actually just a simplified  
example of something I’m doing which isn’t anything to do with People or  
Friends.

Basically I have a recursive object which makes up a tree of nodes –  
imagine friends being of class Person as well (so I guess it would have  
been better to say “has\_many :people” or something). Sometimes I would  
want friends and its all descendants; sometimes I just want friends; and  
sometimes I want friends, and descendants, excluding where a boolean  
attribute is true.

Anyway, thanks for the info; I hadn’t thought of calling finders on a  
collection like that, but I’ve found an adaquate way to solve the  
problem.

Cheers
