Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's dissect that named scope you referenced above. Imagine a model Question which has many Comments.</p> <pre><code>named_scope :most_active, :select =&gt; "questions.*", :joins =&gt; "left join comments as comments_for_count on comments_for_count.question.id = questions.id", :group =&gt; "questions.id", :order =&gt; "count(questions.id) desc" :most_active </code></pre> <p>the name of your scope. You would reference thusly: Question.find(:all).most_active</p> <pre><code>:select =&gt; "questions.*" </code></pre> <p>by default scopes selects all columns from your table anyway, so this limits the results to only the questions table, and not the comments table. This is optional.</p> <pre><code>:joins =&gt; "left join comments as comments_for_count on comments_for_count.question.id = questions.id" </code></pre> <p>this is saying for every question, I also want to get all comments associated with them. The comments table has a column 'question_id' which is what we'll be using to match them up to the appropriate question record. This is important. It allows us access to fields that are not on our model!</p> <pre><code> :group =&gt; "questions.id" </code></pre> <p>This is required for the count() function in the order clause to tell us that we want the count of comments based on question. We don't need the count function in our order clause, so we also don't need this group statement</p> <pre><code>:order =&gt; "count(questions.id) desc" </code></pre> <p>Return the results in order of number of comments, highest to lowest. </p> <p>So for our example, discarding what we don't need, and applying to your needs, we end up with:</p> <pre><code>:named_scope :by_program_manager_name, :joins =&gt; "left join users on projects.program_manager_id = users.id", :order =&gt; "users.name" </code></pre> <p>This named_scope would be called thusly: </p> <pre><code>Project.find(:all).by_program_manager_name </code></pre> <p>Note this is basically equivalent to:</p> <pre><code>Project.find(:all, :joins =&gt; "left join users on projects.program_manager_id = users.id", :order =&gt; "users.name") </code></pre> <p>But, as cam referenced above, you should really know the underlying SQL. Your abilities will be severely hampered without this understanding</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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