# Rindex not working with objects from db model

**URL:** <https://www.ruby-forum.com/t/rindex-not-working-with-objects-from-db-model/140712>\
**Category:** Rails\
**Created:** [June 17, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/rindex-not-working-with-objects-from-db-model/140712 "2008-06-17T20:36:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alais](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@alais](https://www.ruby-forum.com/u/alais)\
**Post date:** [June 17, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/rindex-not-working-with-objects-from-db-model/140712/1 "2008-06-17T20:36:33Z")

</div>

I’m sorting a separate list that doesn’t contain a certain user’s  
attribute, but trying to but that certain user’s attribute on the  
separate percentile.

test\_scores = Testscore.find(:all, :conditions=\> {…})

my\_testscore = current\_user.test\_score  
my\_ts = Testscore.new(:score =\> my\_testscore)

test\_scores.push(ts)

sorted\_ts = test\_scores.sort {|x,y| x.score \<=\> y.score }

index\_one = sorted\_ts.index(my\_ts)

# why doesn’t this work?

index\_two = sorted\_ts.rindex(my\_ts)

It finds index\_one correctly, but index\_two always equals index\_one,  
even if there are more than one object with a score of my\_testscore.

Please help! Thanks.

---

<div class="post-metadata">

**Author:** ![alais](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@alais](https://www.ruby-forum.com/u/alais)\
**Post date:** [June 17, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/rindex-not-working-with-objects-from-db-model/140712/2 "2008-06-17T21:47:42Z")

</div>

On 17 Jun 2008, at 19:36, s. \ wrote:

> test\_scores.push(ts)
> 
> sorted\_ts = test\_scores.sort {|x,y| x.score \<=\> y.score }
> 
> index\_one = sorted\_ts.index(my\_ts)
> 
> # why doesn’t this work?
> 
> index\_two = sorted\_ts.rindex(my\_ts)

> It finds index\_one correctly, but index\_two always equals index\_one,  
> even if there are more than one object with a score of my\_testscore.

ActiveRecord object compare base on their primary key so this is the  
expected behaviour.  
sorted\_ts.rindex {|test\_score| test\_score.score == my\_ts.score} might  
be what you want.

Fred

---

<div class="post-metadata">

**Author:** ![alais](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@alais](https://www.ruby-forum.com/u/alais)\
**Post date:** [June 18, 2008, 5:26am UTC](https://www.ruby-forum.com/t/rindex-not-working-with-objects-from-db-model/140712/3 "2008-06-18T05:26:54Z")

</div>

Thanks. Sorry, but I’m getting an argument error now… it tells me I  
should have 1 argument, not 0?

Frederick C. wrote:

> On 17 Jun 2008, at 19:36, s. \ wrote:
> 
> > test\_scores.push(ts)
> > 
> > sorted\_ts = test\_scores.sort {|x,y| x.score \<=\> y.score }
> > 
> > index\_one = sorted\_ts.index(my\_ts)
> > 
> > # why doesn’t this work?
> > 
> > index\_two = sorted\_ts.rindex(my\_ts)
> 
> > It finds index\_one correctly, but index\_two always equals index\_one,  
> > even if there are more than one object with a score of my\_testscore.
> 
> ActiveRecord object compare base on their primary key so this is the  
> expected behaviour.  
> sorted\_ts.rindex {|test\_score| test\_score.score == my\_ts.score} might  
> be what you want.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![alais](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@alais](https://www.ruby-forum.com/u/alais)\
**Post date:** [June 18, 2008, 7:43am UTC](https://www.ruby-forum.com/t/rindex-not-working-with-objects-from-db-model/140712/4 "2008-06-18T07:43:11Z")

</div>

On 18 Jun 2008, at 04:26, s. wrote:

> Thanks. Sorry, but I’m getting an argument error now… it tells me I  
> should have 1 argument, not 0?

oops, looks like the block version is only available in 1.8.7.

Fred
