Make has_many return only the latest 5 *in order*

In my app, a user has_many resolutions, and i want @user.resolutions to
return only the most recently updated five resolutions, but ordered
oldest first out of those five.

So, let’s say i have 12 records, which we’ll label 1 through to 12. For
simplicity’s sake let’s say that the order of updated_at matches the
order of the labels. Then,

has_many :resolutions, :order => “id DESC”, :limit => 5

gives me [12,11,10,9,8]

This is the most recent five, but i want them ordered the other way,
ie

[8,9,10,11,12]

How do i do this in the has_many method call?

thanks
max

Couldn’t you just do [12,11,10,9,8].reverse ?

Nathan E. wrote:

Couldn’t you just do [12,11,10,9,8].reverse ?

I want to avoid writing .reverse all over my controllers though - i’d
like the ordering to be automatic.

Nathan E. wrote:

Couldn’t you just do [12,11,10,9,8].reverse ?

I want to avoid writing .reverse all over my controllers though - i’d
like the ordering to be automatic.

In your model…

def reversed_resolutions
resolutions.reverse
end

Then you can just reference that.

Philip H. wrote:

Nathan E. wrote:

Couldn’t you just do [12,11,10,9,8].reverse ?

I want to avoid writing .reverse all over my controllers though - i’d
like the ordering to be automatic.

In your model…

def reversed_resolutions
resolutions.reverse
end

Then you can just reference that.

Again, i don’t want to write reversed_resolutions all over my
controllers. It’s unDRY and liable to introduce errors.

def resolutions
Resolution.find_by_user_id(id, :order => “id DESC”, :limit =>
5).reverse
end

If you’re not willing to accept people’s answers then don’t ask
questions.

On Dec 20, 2007 8:56 AM, Max W. [email protected]
wrote:

def reversed_resolutions


Ryan B.

If you want that functionality then you’re going to have to call
.reverse in
your controllers… or you could do something extremely hackish:
alias_method :associated_resolutions, :resolutions
def resolutions
associated_resolutions.reverse
end

This should still allow you to have the << and = functionality with
resolutions, but allow you to display them in reverse order. Put the
order
and the limit back onto the has_many. I don’t know why I didn’t think of
it
before.

On Dec 20, 2007 7:55 PM, Max W. [email protected]
wrote:

@user.resolutions << resolution

Ie it adds the new resolution to the local returned array from the
method, but not to the database.

thanks again, max

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


Ryan B.

Ryan B. wrote:

def resolutions
Resolution.find_by_user_id(id, :order => “id DESC”, :limit =>
5).reverse
end

If you’re not willing to accept people’s answers then don’t ask
questions.

Ryan B.
http://www.frozenplague.net

Thanks Ryan. Sorry to those earlier posters, i didn’t mean to sound
rude (just comes natural to an ass like me).

I actually tried the above, ie replacing has_many with a regular
instance method, but then i found i was having problems doing stuff like
this

@user.resolutions << resolution

Ie it adds the new resolution to the local returned array from the
method, but not to the database.

thanks again, max

On 19 Dec 2007, at 21:23, Max W. wrote:

has_many :resolutions, :order => “id DESC”, :limit => 5

A bit nasty, but how about

has_many :resolutions, :finder_sql => ‘SELECT * from (SELECT * from
resolutions order by id desc limit 5) as t order by id asc’

Fred

Ryan B. wrote:

If you want that functionality then you’re going to have to call
.reverse in
your controllers… or you could do something extremely hackish:
alias_method :associated_resolutions, :resolutions
def resolutions
associated_resolutions.reverse
end

This should still allow you to have the << and = functionality with
resolutions, but allow you to display them in reverse order. Put the
order
and the limit back onto the has_many. I don’t know why I didn’t think of
it
before.

On Dec 20, 2007 7:55 PM, Max W. [email protected]
wrote:

@user.resolutions << resolution

Ie it adds the new resolution to the local returned array from the
method, but not to the database.

thanks again, max

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


Ryan B.
http://www.frozenplague.net

ah - does that mean that “associated_whatevers” is an under the hood
rails method used when we do stuff like

@user.resolutions << resolution

? ie something that’s set up by ‘has_many’?

Frederick C. wrote:

On 19 Dec 2007, at 21:23, Max W. wrote:

has_many :resolutions, :order => “id DESC”, :limit => 5

A bit nasty, but how about

has_many :resolutions, :finder_sql => ‘SELECT * from (SELECT * from
resolutions order by id desc limit 5) as t order by id asc’

Fred

ah i never thought about simply doing it all in sql :slight_smile:

cheers

Ryan B. wrote:

associated_resolutions is what alias_method duplicates resolutions into.

Ah right, i see what you mean. :slight_smile:

thanks.

associated_resolutions is what alias_method duplicates resolutions into.