# Has\_many, find\_all problem

**URL:** <https://www.ruby-forum.com/t/has-many-find-all-problem/86020>\
**Category:** Rails\
**Created:** [February 19, 2007, 4:17am UTC](https://www.ruby-forum.com/t/has-many-find-all-problem/86020 "2007-02-19T04:17:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 19, 2007, 4:17am UTC](https://www.ruby-forum.com/t/has-many-find-all-problem/86020/1 "2007-02-19T04:17:42Z")

</div>

ruby 1.8.5, Rails 1.2.1  
Caution - I’m a ruby noob.

I have 2 models - Meeting and Meeting\_Items.  
As you would expect a meeting has\_many :meeting\_items and a  
meeting\_item belongs\_to :meeting.

When trying to use find\_all to return an array which only includes  
“agenda items” I found that it always returned the full array of  
meeting\_items.  
To try to find the fault I have reduced the problem to its’ simplest  
form that still gives me the problem.

@meeting.meeting\_items.find\_all{ |m| false}

The code above still returns the full array of meeting\_items for the  
meeting, I would expect it to return an empty array.

When I do the following I get an empty array returned, which is what I  
expect.  
[1,2,3,4,5].find\_all{|x| false}

Any help appreciated.

Reed

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 19, 2007, 4:32am UTC](https://www.ruby-forum.com/t/has-many-find-all-problem/86020/2 "2007-02-19T04:32:43Z")

</div>

On 2/19/07, reed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “agenda items” I found that it always returned the full array of  
> meeting\_items.  
> To try to find the fault I have reduced the problem to its’ simplest  
> form that still gives me the problem.
> 
> @meeting.meeting\_items.find\_all{ |m| false}

The find\_all is an Array method but meeting\_items is not strictly an  
array  
even though it behaves a bit like one. The find all on a collection of  
AR  
models is a depricated method. Intead you should use find(:all,  
:conditions =\> … )

The code above still returns the full array of meeting\_items for the

> meeting, I would expect it to return an empty array.
> 
> When I do the following I get an empty array returned, which is what I  
> expect.  
> [1,2,3,4,5].find\_all{|x| false}
> 
> Any help appreciated.
> 
> Reed

I think there are a couple of ways to achive this functionality i think  
you’re looking for…

Using  
Meeting.find(:all, :conditions =\> { :field =\> ‘value’ } )

Or you can set this up in the meeting class as a has\_many

class Meeting \< AR::B  
has\_many :meeting\_items  
has\_many :agenda\_items, :class\_name =\> ‘meeting\_item’, :conditions =\>  
“field=‘value’”  
end

then call this by  
@meeting.agenda\_items

I hope this helps.

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 19, 2007, 4:56am UTC](https://www.ruby-forum.com/t/has-many-find-all-problem/86020/3 "2007-02-19T04:56:44Z")

</div>

Wow, thanks for the info.

has\_many :agenda\_items, :class\_name =\> ‘meeting\_item’, :conditions =\>

… is exactly what I am after but I was implementing it in a lame  
manner, when compared to this solution.

Cheers,  
Reed

---

<div class="post-metadata">

**Author:** ![reed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9de0a6/32.png) [@reed](https://www.ruby-forum.com/u/reed)\
**Post date:** [February 19, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/has-many-find-all-problem/86020/4 "2007-02-19T14:56:50Z")

</div>

another thing you might want to look into is using association  
extensions

has\_many :items, :class\_name =\> ‘meeting\_item’ do  
def agendas  
find(:all, :conditions =\> …)  
end

def some\_other\_type  
find(:all, :conditions =\> …)  
end  
end

what’s nice about this is you can now say

# all meeting items

meeting.items

# all agenda meeting items

meeting.items.agendas

# all ‘some\_other\_type’ items

meeting.items.some\_other\_type
