Last active
May 10, 2020 06:45
-
-
Save inopinatus/1e5b70ca30c11c04b99ddf70274c04ea to your computer and use it in GitHub Desktop.
arel "most frequent"
This file contains 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
module FrequencyExtension | |
def most_frequent | |
frequency_order.take | |
end | |
def frequency_order | |
group(arel_attribute(primary_key).order(arel_attribute(primary_key).count.desc) | |
end | |
end |
This file contains 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
class User | |
has_many :clubs | |
has_many :sports, through: :clubs, extend: FrequencyExtension | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@user.sports.most_frequent