How do you order an array by a connected integer in Ruby on Rails? -


I am creating the most popular activity section for a user profile. I have no difficulty in dragging questions through user_id but I am having trouble pulling from related integers, respectively: question.votes.size This is probably just a question but how do I sort out the output by limiting it to 3 Can i How can I do without the freezing of the database? At the end, a lot of votes should be counted, it should be a named_scope?

  @user_id = User.find_by_username (params [: user name]). ID Question = Question.find (: All ,: conditions = & gt; {: user_id = & gt;; @user_id})  

I want to suggest another method in pop, maybe, a little more native ROR: -)

@user = User.find_by_username (params [: Username] ,: include => [{: question =>: vote}]) @sorted_questions = @ user.questions.sort {| Q1, q2 Q2.votes.length & lt; => Q1.votes.length}

There are several advantages:

1) No SQL written, DB maintains portability, easy to read

And 1) Some disadvantages:

1) Low latency on low load, less efficient on one box 2) Transfers more data, potential profit reduces to # 2

Ideally, if you want to see ActiveRecord's counter cache functionality, this parent ties the relationship by denormalizing the child's row count in the table. To make this work, all the original line manipulation should be generated through the original object, but in any case the rail is the best practice.

Questions referencing the vote table by having a vote count in cash. To do this, and rubbing in Ruby, the attitude of both the display and the code can be the ideal situation.

In the end, I have to accept stripping on tracks 3 very quiet relational algebra stuff. With it, it is likely that it can be a super-readable one-liner that generates optimal SQL. How cool is it going to be? : -)


Comments