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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 17 Jun 2008, at 19:36, s. \ wrote:> > 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. >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> Please help! Thanks. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks. Sorry, but I''m getting an argument error now... it tells me I should have 1 argument, not 0? Frederick Cheung 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> Frederick Cheung 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 > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---