Last active
December 20, 2015 08:59
-
-
Save rentalcustard/6104481 to your computer and use it in GitHub Desktop.
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
1.9.3p448 :007 > Resource.all.sort_by(&:average_rating) | |
Resource Load (0.4ms) SELECT "resources".* FROM "resources" | |
Rating Load (0.3ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."resource_id" = $1 ORDER BY "ratings"."id" ASC LIMIT 1 [["resource_id", 1]] | |
Rating Load (0.2ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."resource_id" = $1 [["resource_id", 1]] | |
Rating Load (0.2ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."resource_id" = $1 ORDER BY "ratings"."id" ASC LIMIT 1 [["resource_id", 2]] | |
Rating Load (0.2ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."resource_id" = $1 [["resource_id", 2]] | |
=> [#<Resource id: 2, title: nil, summary: nil, link: nil, created_at: "2013-07-29 13:53:52", updated_at: "2013-07-29 13:53:52", user_id: nil>, #<Resource id: 1, title: nil, summary: nil, link: nil, created_at: "2013-07-29 13:52:36", updated_at: "2013-07-29 13:52:36", user_id: nil>] |
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 Resource < ActiveRecord::Base | |
def self.all_with_rating | |
select('resources.id, avg(ratings.value) as avg_rating'). | |
joins(:ratings). | |
order('avg(ratings.value)'). | |
group('resources.id') | |
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
1.9.3p448 :001 > Resource.all | |
Resource Load (0.4ms) SELECT "resources".* FROM "resources" | |
=> #<ActiveRecord::Relation [#<Resource id: 1, title: nil, summary: nil, link: nil, created_at: "2013-07-29 13:52:36", updated_at: "2013-07-29 13:52:36", user_id: nil>, #<Resource id: 2, title: nil, summary: nil, link: nil, created_at: "2013-07-29 13:53:52", updated_at: "2013-07-29 13:53:52", user_id: nil>]> | |
1.9.3p448 :002 > Rating.all | |
Rating Load (0.3ms) SELECT "ratings".* FROM "ratings" | |
=> #<ActiveRecord::Relation [#<Rating id: 1, user_id: nil, resource_id: 1, value: 4, created_at: "2013-07-29 13:52:53", updated_at: "2013-07-29 13:53:11">, #<Rating id: 2, user_id: nil, resource_id: 1, value: 3, created_at: "2013-07-29 13:53:26", updated_at: "2013-07-29 13:53:26">, #<Rating id: 3, user_id: nil, resource_id: 2, value: 2, created_at: "2013-07-29 13:54:02", updated_at: "2013-07-29 13:54:02">, #<Rating id: 4, user_id: nil, resource_id: 2, value: 1, created_at: "2013-07-29 13:54:04", updated_at: "2013-07-29 13:54:04">, #<Rating id: 5, user_id: nil, resource_id: 2, value: 3, created_at: "2013-07-29 13:54:06", updated_at: "2013-07-29 13:54:06">]> | |
1.9.3p448 :006 > Resource.select('resources.id, avg(ratings.value) as avg_rating').joins(:ratings).order('avg(ratings.value)').group('resources.id').map {|r| [r.id, r.avg_rating.to_s] } | |
Resource Load (0.6ms) SELECT resources.id, avg(ratings.value) as avg_rating FROM "resources" INNER JOIN "ratings" ON "ratings"."resource_id" = "resources"."id" GROUP BY resources.id ORDER BY avg(ratings.value) | |
=> [[2, "2.0"], [1, "3.5"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment