I am using Rails 4.2 and have set up the impressionist gem which is working fine for logging page impressions.
I am trying to do exactly the same thing as in this Stackoverflow post but the answer isn't working. I tried:
start_time = 30.days.ago
@mostReadAlbums30Days = Album.joins(:impressions).where("impressions.created_at<='#{Time.now}' and impressions.created_at >= '#{start_time}'").group("impressions.impressionable_id").order("count(impressions.id) DESC")
This produces the following SQL and error message
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "impressions" LINE 1: ... BY impressions.impressionable_id ORDER BY count(impression...
^ : SELECT "albums".* FROM "albums" INNER JOIN "impressions" ON "impressions"."impressionable_id" = "albums"."id" AND "impressions"."impressionable_type" = $1 WHERE (impressions.created_at<='2015-01-02 00:50:18 -0200' and impressions.created_at >= '2014-12-03 02:50:18 UTC') GROUP BY impressions.impressionable_id ORDER BY count(impressions.id) DESC
I'm using Postgresql v9.3.5.2. How can I get this query to work?
ORDER BY count(impressions.id). Look at howimpressionsgets line-broken in the rendering of the error message, that wouldn't happen otherwise. - Daniel Vérité