简体   繁体   English

Rails印象派瑰宝-缺少表“ impressions”的FROM子句条目

[英]Rails impressionist gem - missing FROM-clause entry for table “impressions”

I am using Rails 4.2 and have set up the impressionist gem which is working fine for logging page impressions. 我正在使用Rails 4.2,并设置了印象派 gem,它可以很好地记录页面印象。

I am trying to do exactly the same thing as in this Stackoverflow post but the answer isn't working. 我正在尝试做与此Stackoverflow帖子中完全相同的事情,但是答案不起作用。 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(impression‌​s.id) DESC")

This produces the following SQL and error message 这将产生以下SQL和错误消息

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "impression‌​s" LINE 1: ... BY impressions.impressionable_id ORDER BY count(impression... PG :: UndefinedTable:错误:缺少表“ impression‌s”的FROM子句条目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(impression‌​s.id) DESC ^:选择“相册”。*从“相册”内联接“印象”到“印象”。“ impressionable_id” =“相册”。“ id”和“印象”。“ impressionable_type” = $ 1哪里(impressions.created_at <= '2015-01-02 00:50:18 -0200'和impressions.created_at> ='2014-12-03 02:50:18 UTC')GROUP BY impressions.impressionable_id ORDER BY count(impression‌s.id)DESC

I'm using Postgresql v9.3.5.2. 我正在使用Postgresql v9.3.5.2。 How can I get this query to work? 我怎样才能使该查询工作?

The main problem (per comment): 主要问题(每条评论):

There's a subtle typo in your query. 您的查询中有一个细微的错字。 It contains a Unicode Character ZERO WIDTH SPACE (U+200B) between the n and s of ORDER BY count(impression‌s.id) . 它在ORDER BY count(impression‌s.id)的n和s之间包含一个Unicode字符零宽度空格(U + 200B ORDER BY count(impression‌s.id) Look at how impressions gets line-broken in the rendering of the error message, that wouldn't happen otherwise. 看看在错误消息的呈现中impressions如何断行,否则是不会发生的。


The GROUP BY problem: since the query selects albums.* , it has to GROUP BY albums.id rather than GROUP BY impressions.impressionable_id (assuming that albums.id is a primary key). GROUP BY问题:由于查询选择了albums.* ,因此它必须使用GROUP BY albums.id而不是GROUP BY impressions.impressionable_id (假设albums.id是主键)。 This is numerically equivalent since the tables are equi-joined through these two fields anyway, but as it seems, the SQL engine in this context can't figure out this equivalency by itself. 这在数值上是等效的,因为无论如何这些表都是通过这两个字段进行等价联接的,但是看起来,在这种情况下,SQL引擎本身无法确定这种等效性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM