cmaxvv
December 19, 2007, 10:23pm
1
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
cmaxvv
December 19, 2007, 10:27pm
2
Couldn’t you just do [12,11,10,9,8].reverse ?
cmaxvv
December 19, 2007, 10:59pm
3
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.
cmaxvv
December 19, 2007, 11:05pm
4
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.
cmaxvv
December 19, 2007, 11:26pm
5
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.
cmaxvv
December 19, 2007, 11:48pm
6
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.
cmaxvv
December 20, 2007, 12:19pm
7
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.
cmaxvv
December 20, 2007, 10:25am
8
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
cmaxvv
December 20, 2007, 1:01pm
9
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
cmaxvv
December 20, 2007, 12:39pm
10
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’?
cmaxvv
December 20, 2007, 1:46pm
11
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
cheers
cmaxvv
December 21, 2007, 10:19am
12
Ryan B. wrote:
associated_resolutions is what alias_method duplicates resolutions into.
Ah right, i see what you mean.
thanks.
cmaxvv
December 20, 2007, 8:39pm
13
associated_resolutions is what alias_method duplicates resolutions into.