I tried to connect Arel queries with scopes, instead of just using some long logic that I wrote in the controller. But the scope is slower than just getting all the records and then sifting through them using some logic. Then I wonder why areas are better.
That's what I'm doing:
- There are many answers to the question.
- the answer belongs to one question
- The question has a column "question_type", which I use to sort it
First, the path of the area ...
in the .rb question:
scope :answered, joins(:answers).order('answers.created_at desc') scope :dogs, where(:question_type => "dogs") scope :cats, where(:question_type => "cats") scope :mermaids, where(:question_type => "mermaids")
in questions_controller.rb:
@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]
Then in the view, I look at these instance variables (which are now arrays containing no more than 10 elements) and display the results.
Here are a few times to load the page (five times):
Completed 200 OK at 535ms (Views: 189.6ms | ActiveRecord: 46.2ms)
Completed 200 OK in 573ms (Views: 186.0ms | ActiveRecord: 46.3ms)
Completed 200 OK in 577ms (Views: 189.0ms | ActiveRecord: 45.6ms)
Completed 200 OK at 532ms (Views: 182.9ms | ActiveRecord: 46.1ms)
Completed 200 OK in 577ms (Views: 186.7ms | ActiveRecord: 46.9ms)
Now, the way of the messy controller ...
@answers = Answer.order("created_at desc") @all_answered = [] @answers.each {|answer| @all_answered << 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 << q @dogs_recently_answered = @dogs_all_answered[0..9] elsif q.question_type == "cats" @cats_all_answered << q @cats_recently_answered = @cats_all_answered[0..9] elsif q.question_type == "mermaids" @mermaids_all_answered << q @mermaids_recently_answered = @mermaids_all_answered[0..9] end end
And here is the time it takes to load the page now (five times):
Completed 200 OK in 475ms (Views: 196.5ms | ActiveRecord: 34.5ms)
Completed 200 OK in 480ms (Views: 200.4ms | ActiveRecord: 36.4ms)
Completed 200 OK in 434ms (Views: 198.2ms | ActiveRecord: 35.8ms)
Completed 200 OK in 475ms (Views: 194.2ms | ActiveRecord: 36.4ms)
Completed 200 OK in 475ms (Views: 195.0ms | ActiveRecord: 35.4ms)
So...
In addition to readability, what can be gained by honing the request with scope? Does it get faster when there are more entries?