Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are Rails scopes preferable, if messy controllers are faster?
    primarykey
    data
    text
    <p>I've been trying to chain Arel queries using scopes, instead of just using some long-winded logic I wrote in the controller. But the scopes are slower than just getting all the records and then sifting through them with some logic. I'm wondering, then, why scopes are better.</p> <p>Here's what I'm doing:</p> <ul> <li>a question has many answers</li> <li>an answer belongs to one question</li> <li>a question has a "question_type" column that I use to sort it</li> </ul> <p><strong>First, the scopes way...</strong></p> <p>in question.rb:</p> <pre><code>scope :answered, joins(:answers).order('answers.created_at desc') scope :dogs, where(:question_type =&gt; "dogs") scope :cats, where(:question_type =&gt; "cats") scope :mermaids, where(:question_type =&gt; "mermaids") </code></pre> <p>in questions_controller.rb:</p> <pre><code>@dogs_recently_answered = Question.answered.dogs.uniq[0..9] @cats_recently_answered = Question.answered.cats.uniq[0..9] @mermaids_recently_answered = Question.answered.mermaids.uniq[0..9] </code></pre> <p>Then in the view, I cycle through those instance variables (which are now arrays containing at most 10 elements) and display the results.</p> <p><em>Here are the times it takes to load the page (five different times):</em></p> <p>Completed 200 OK in 535ms (Views: 189.6ms | ActiveRecord: 46.2ms)</p> <p>Completed 200 OK in 573ms (Views: 186.0ms | ActiveRecord: 46.3ms)</p> <p>Completed 200 OK in 577ms (Views: 189.0ms | ActiveRecord: 45.6ms)</p> <p>Completed 200 OK in 532ms (Views: 182.9ms | ActiveRecord: 46.1ms)</p> <p>Completed 200 OK in 577ms (Views: 186.7ms | ActiveRecord: 46.9ms)</p> <p><strong>Now, the messy controller way...</strong></p> <pre><code>@answers = Answer.order("created_at desc") @all_answered = [] @answers.each {|answer| @all_answered &lt;&lt; answer.question} @recently_answered = @all_answered.uniq @dogs_all_answered = [] @cats_all_answered = [] @mermaids_all_answered = [] @recently_answered.each do |q| if q.question_type == "dogs" @dogs_all_answered &lt;&lt; q @dogs_recently_answered = @dogs_all_answered[0..9] elsif q.question_type == "cats" @cats_all_answered &lt;&lt; q @cats_recently_answered = @cats_all_answered[0..9] elsif q.question_type == "mermaids" @mermaids_all_answered &lt;&lt; q @mermaids_recently_answered = @mermaids_all_answered[0..9] end end </code></pre> <p><em>And here are the times it takes to load the page now (five different times):</em></p> <p>Completed 200 OK in 475ms (Views: 196.5ms | ActiveRecord: 34.5ms)</p> <p>Completed 200 OK in 480ms (Views: 200.4ms | ActiveRecord: 36.4ms)</p> <p>Completed 200 OK in 434ms (Views: 198.2ms | ActiveRecord: 35.8ms)</p> <p>Completed 200 OK in 475ms (Views: 194.2ms | ActiveRecord: 36.4ms)</p> <p>Completed 200 OK in 475ms (Views: 195.0ms | ActiveRecord: 35.4ms)</p> <p><strong>So...</strong></p> <p>Aside from readability, what's to be won by honing the query with a scope? Does it eventually become quicker when there are more records?</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. 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