Sort on dates from has_many?

Hey all,

First, still getting into Ruby/Rails, and am drinking from a firehose. A lot of good information on this list if you can get to it!

I have an Event model with many Showings. The Showings are simply dates that are related to the Event.

I'm trying to get the Events in the order of the first Showing date, and not have them repeat.

Right now, I have this in my index:

    @allShowings = Showing.find(:all, :order => 'date')

    @events = Array.new
    @showings = Array.new
    @shown = Hash.new

    @allShowings.each do |showing|
      unless @shown.has_key?(showing.event_id)
        @events << Event.find_by_id(showing.event_id)
        @showings << showing
      end

      @shown[showing.event_id] = 1;
    end
  end

It works, but I need the same sorted data in other places. Should I have a private method that returns a sorted list of Events like this?

Is there some way I can use the Showing date order when I'm doing the Event.find?

  Event.find(:all, :order => (magic pulling the order from Showings goes here))

Thanks,
Wyatt

in the Event model, you could say
  has_many :showings, :order => 'date'

then perhaps make a finder
def self.find_ordered
   Event.find(:all, :order => 'showings.date', :include => :showings)
end

use in a controller:
@ordered_events = Event.find_ordered

the ":order" clause in the model's association macro isn't necessary
to make the custom finder, but it will help if you always want
event.showings to default to date order

That's exactly what I was looking for.

Thank you!