# Conditionalizing find via has\_many

**URL:** <https://www.ruby-forum.com/t/conditionalizing-find-via-has-many/108071>\
**Category:** Rails\
**Created:** [June 28, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/conditionalizing-find-via-has-many/108071 "2007-06-28T21:00:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [June 28, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/conditionalizing-find-via-has-many/108071/1 "2007-06-28T21:00:30Z")

</div>

I have a has\_many relationship (person has\_many meetings). The majority  
of people in my system have no meetings, so I only want to deal with  
Person where meetings.size \> 0. Is there a simple and elegant way to  
manipulate the find() method I’d normally use, possibly via :include and  
:conditions, so that I can use this as needed without having to resort  
to SQL? I get the concept of Person.find(:include=\>[‘meetings’]) to  
hook the two up and make meetings accessible, but :conditions deals with  
a direct SQL statement and not a Ruby statement, doesn’t it?

It would be even better if I could do something equivalent to  
redefining find(:all) so that this only ever deals with that particular  
condition.

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [February 27, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/conditionalizing-find-via-has-many/108071/2 "2008-02-27T15:03:56Z")

</div>

Hi Duane,

Did you manage to solve this, I’ve got a similar problem now that I’m  
trying to work out

Thanks,  
George.

On Jun 28 2007, 7:00 pm, Duane M. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [February 27, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/conditionalizing-find-via-has-many/108071/3 "2008-02-27T16:33:45Z")

</div>

Duane M. wrote:

> It would be even better if I could do something equivalent to  
> redefining find(:all) so that this only ever deals with that particular  
> condition.

As far as I know, there is no AR mechanism to use pure ruby syntax to  
perform a search through a relation whether that is has\_many,  
belongs\_to, etc…

However, this may be close to what you are looking for:

class Person \< ActiveRecord::Base  
def self.with\_meeting\_scope  
with\_scope(:find =\> {:joins =\> “meetings on meetings.person\_id =  
people.id”}) {yield}  
end

def self.find\_all\_with\_meetings  
with\_meeting\_scope {find :all}  
end  
end

I myself however would just use the sql as I find that the with\_scope  
syntax can quickly get out of hand and it usually turns out to be vendor  
specific anyways…

hth

ilan
