Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't convert hash to integer - Trying to select random record
    primarykey
    data
    text
    <p>I'm trying to select a random record, but the only working method seems like a horrible performance liability.</p> <p>First attempt, using RANDOM function from Postgres - throws "Can't convert Hash into Integer"</p> <pre><code> assigned_specialist = User.specialists.legal_for(coach_section).experienced_in(critique.discipline_id).first(:order =&gt; "RANDOM()") return assigned_specialist.id </code></pre> <p>Second attempt, using <a href="https://stackoverflow.com/questions/2752231/random-record-in-activerecord">offset method discussed here.</a> Also causes "Can't convert Hash into Integer"</p> <pre><code> legal_specialists = User.specialists.legal_for(coach_section).experienced_in(critique.discipline_id) offset = rand(legal_specialists.count) assigned_specialist = legal_specialists.first(:offset =&gt; offset) return assigned_specialist.id </code></pre> <p>Final attempt, using array sample method, which works but seems bad.</p> <pre><code> legal_specialists = User.specialists.legal_for(coach_section).experienced_in(critique.discipline_id) assigned_specialist = legal_specialists.sample return assigned_specialist.id </code></pre> <p>To be fair, there will only be about 20 specialists in the system, so the array sample method might never cause any issues, but I'd still like to understand what's going on here.</p> <p><strong>Edit</strong>: Here are the scopes I defined: </p> <pre><code>scope :specialists, where(:role_id =&gt; 2) scope :legal_for, lambda { |coach_section| where("section_id != ?", coach_section) } scope :experienced_in, lambda { |discipline_id| discipline = Discipline.find(discipline_id) discipline.users } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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