Organize by a field

i have a field release_date under the table album that belongs_to:
artist

and i now have a page that outputs:
release_date
artist - album

how can i organize them by that release date?

here’s my code at the moment

<% for artist in @artists %>

<% for album in artist.albums %>
<%= album.release_date %>

<%= artist.artist %> - <%= album.album %> - <%= link_to ‘View Reviews’,
:action => ‘showalbum’, :id => album %>

<% end %>

<% end %>

On 11/18/06, Matt M. [email protected] wrote:

here’s my code at the moment


Posted via http://www.ruby-forum.com/.

Matt,

You could do a sort like this:

<% for album in artist.albums.sort_by{ |a| a.release_date } %>

or better yet look at adjusting your has_many call to:

has_many :albums, :order => ‘release_date’

Hope this helps.


Zack C.
http://depixelate.com

that works for sorting just the albums, but it sorts the artists
alphabetically, then it sorts the releases under that. could i get it to
sort everything by release, ignoring the alphabetical artist sorting, so
something i could put in <% for artist in @artists %>

Zack C. wrote:

On 11/18/06, Matt M. [email protected] wrote:

here’s my code at the moment


Posted via http://www.ruby-forum.com/.

Matt,

You could do a sort like this:

<% for album in artist.albums.sort_by{ |a| a.release_date } %>

or better yet look at adjusting your has_many call to:

has_many :albums, :order => ‘release_date’

Hope this helps.


Zack C.
http://depixelate.com

On 11/18/06, Matt M. [email protected] wrote:


Posted via http://www.ruby-forum.com/.

Matt,

I don’t understand exactly what you want here… if we have two
artists with release dates like this:

Artist 1 has albums with release_dates of 1/1/06, 3/1/06, 5/1/06
Artist 2 has albums with release_dates of 2/1/06, 4/1/06

then <% for artist in @artists %> won’t work because they have
releases that stagger. What you want to do if the primary sort is
release date is something like the following:

@albums = Album.find(:all, :include => :artist, :order =>
‘release_date’)

then

<% for album in @albums -%>
<%= album.release_date.strftime(…) %> - <%= album.artist.name %>
<% end -%>

Does that make sense?


Zack C.
http://depixelate.com