How do I find the items immediately before and after a specific one in an ordered array, using Ruby on Rails? -


I have an array of 'questions', which are ordered according to the number of their votes, and immediately and immediately before Want to show the question after the currently selected question?

Assume that the currently selected question is stored in the variable @question, and I am showing a list of other questions related to that user. This code gives order according to the number of votes:

  question = question.fund (: all, conditions => gt; {: user = & gt; @ question.user}) . Sort {| Q1, Q2. Q2.votes.length & lt; = & Gt; Q1.votes.length}  

How can I raise the question before and after that question?

I updated my reply, I thought you would get summaries from the first post, but here's a more detailed Example.

You can not do something simple:

  @questions = user .questions.sort_by {| Q | | -q.votes.length} current_question_index = @ questions.index (@question) @prev_question = @questions [current_question_index-1] @next_question = @ [current_question_index + 1]  

this more Lines use simple array manipulation


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -