I have an association between the scout table and the rank table but this is a many to many relationship. I didn't want to use HABTM because I wanted to keep the rank_date for each rank.
My model now has three tables for this relationship: scout, rank_scout, and rank. Frequently I want to list the current rank of a scout and with DRY, I don't want the find method all over the place. I added a method to the scout model to get the current_rank and also the current_rank_scout model.
To get the current_rank_scout, I need to select the row with the maximum rank_date.
I tried lots of different combinations (in all of these, 'scout' is the current scout model)
scout.rank_scouts.maximum(:rank_date)but this gave me the maximum value of rank date for the scout, close but I need the whole row.
scout.rank_scouts(:max => :rank_date)Nope, not even close there.
scout.rank_scouts.find(:conditions => {'max(rank_date)'})
but :conditions must be a hash (and even if it was a hash, this doesn't work right.
Of course, I'm just making this too hard. Thinking of it a different way, if I order the rows by rank date, I want the first row.
scout.rank_scouts.find(:first, :order => 'rank_date DESC')Ah Ha! That's what I needed. On to the next task.