Forum: Ruby on Rails has_many, find_all problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bfb4faac1ebe6bcd236d65cf98428632?d=identicon&s=25 reed (Guest)
on 2007-02-19 04:17
(Received via mailing list)
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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-02-19 04:32
(Received via mailing list)
On 2/19/07, reed <reedrob@gmail.com> 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.
Bfb4faac1ebe6bcd236d65cf98428632?d=identicon&s=25 reed (Guest)
on 2007-02-19 04:56
(Received via mailing list)
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-02-19 14:56
(Received via mailing list)
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
This topic is locked and can not be replied to.