Last active
August 29, 2015 13:55
-
-
Save pawel2105/8780769 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given the following domain and scenario: | |
Topic has_many votes | |
A rails noob that has a massive dataset but little understanding into ActiveRecord query methods | |
and little SQL knowledge. | |
Using elegant and idiomatic Ruby, what is a good solution for returning all topics sorted by the | |
number of votes they have. Because of the SQL knowledge gap, you should avoid having to use SQL directly. |
If you have 5 million topics and hundreds of votes each, surely you'd want the ordering to be done in the DB?
But if you do have 5 million rows any kind of SQL ordering sucks as well. In those circumstances I'd probably look at using a full-blown search engine.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pixeltrix
One issue I still have with this is that it's using the block to do the sorting. If you have 5 million topics and hundreds of votes each, surely you'd want the ordering to be done in the DB? I know then most people would say then you should optimize your query to use raw SQL but I feel that there is something missing that abstracts that and should belong in
ActiveRecord::QueryMethods
or somewhere.