Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, should this:</p> <pre><code>Participation belongs_to :tournament belongs_to :players </code></pre> <p>be this?</p> <pre><code>Participation belongs_to :tournament belongs_to :player </code></pre> <p>Ie, singular player after the belongs_to?</p> <p>I'm struggling to get my head around what this is doing:</p> <pre><code>class Participation def rank_at_tour(tour) tour.participations.index(self) end end </code></pre> <p>You don't really explain enough about your schema to make it easy to reverse engineer. Is it doing the following...?</p> <p>"Get all the participations for the given tour and return the position of this current participation in that list"? Is that how you calculate rank? If so i agree it seems like a very convoluted way of doing it.</p> <p>Do you do the above for the ten participation objects you get back for the player and then take the average? What is rating? Does that have anything to do with rank? Basically, can you explain your schema a bit more and then restate what you want to do? </p> <p>EDIT</p> <p>I think you just need a more efficient way of finding the position. There's one way i could think of off the top of my head - get the record you want and then count how many are above it. Add 1 to that and you get the position. eg</p> <pre><code>class Participation def rank_at_tour(tour) tour.participations.count("rating &gt; ?", self.rating) + 1 end end </code></pre> <p>You should see in your log file (eg while experimenting in the console) that this just makes a count query. If you have an index on the rating field (which you should have if you don't) then this will be a very fast query to execute.</p> <p>Also - if tour and tournament are the same thing (as i said you seem to use them interchangeably) then you don't need to pass tour to participation since it belongs to a tour anyway. Just change the method to rank:</p> <pre><code>class Participation def rank self.tour.participations.count("rating &gt; ?", self.rating) + 1 end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload