Forum: Ruby on Rails Ordering one model class by details contained in another

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.
8a06d940b20d983af0e6500f865b5e54?d=identicon&s=25 Eric Nielsen (Guest)
on 2006-05-22 18:45
I'm very new to rails.  I have on "main" model class "Competition" with
a supporting model class "CompDates" arranged in a 1:m relationship.  In
my "list" action, I would like to order the competitions by earliest
date.

In other languages, I probably would have simply created a view in the
database.  If I'm trying to limit myself to migration supported database
usage though, that gets a little "dangerous" as the only way it appears
to make a view within the migration is using execute, which loses the
database agnostic nature to some degree.

I suspect I could easily sort them returned competitions in memory, but
that would break the pagination support that the scaffold provided  and
I don't currently see a why to make a post-retrieval sort play nice with
any pagination scheme I could think of.

I tried a rather messy :conditions clausee on the data source for the
paginaite, but as it had to use a MIN(event_date) type construct it fell
apart unless I switched to a find_by_sql style call which felt
inelegant.

I'm currently playing with making the aggregation :act_as_list, while
adding an ordinal to control the ordering of naturally sortable dates
feels a little odd, at least I could right the :condition without
needing to use an aggregate.

Are there other approaches I should consider?

Thanks
Eric
This topic is locked and can not be replied to.