Rails 查询问题

发布于 2024-11-06 10:29:37 字数 712 浏览 1 评论 0原文

我的照片有很多评论。

我想选择任何有最近评论的照片,并以一种“时间轴”的方式显示这些照片,其中最近评论的照片位于顶部,其他照片位于下方。

我尝试了这个,它在 SQLite 上工作:

@photos = Photo.select('DISTINCT photos.*').joins(:comments).order('comments.created_at DESC')

但是在 PostgreSQL 上测试会引发此错误:

PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
\n: SELECT DISTINCT photos.* FROM \"photos\" INNER JOIN \"comments\" ON \...

所以,问题是,我选择照片,但按评论的新近度排序...而 Postgre 不喜欢这样。

任何人都可以建议:

A:我如何解决此查询...

B:通过评论的新近程度检索照片的不同方式?

我这样做而不是通过评论模型的重要原因是我想将每张照片显示一次,并在其旁边显示最近的评论,而不是单独显示每条评论,并多次出现相同的照片。

谢谢!

I have photos which have_many comments.

I want to select whatever photos have recent comments and display those photos in a kind of "timeline" where the most recently commented photo is at the top and other photos fall below.

I tried this, and it worked on SQLite:

@photos = Photo.select('DISTINCT photos.*').joins(:comments).order('comments.created_at DESC')

However testing on PostgreSQL raises this error:

PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
\n: SELECT DISTINCT photos.* FROM \"photos\" INNER JOIN \"comments\" ON \...

So, the problem is, I'm selecting Photos but ordering by recency of comments... and Postgre doesn't like that.

Can anyone suggest either:

A: How I can fix this query...

or

B: A different way to retrieve photos by the recency of their comments?

The important reason I'm doing it this way instead of through the comments model is I want to show each photo once with any recent comments beside it, not show each comment by itself with the same photos appearing multiple times.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷︶言冷语的世界 2024-11-13 10:29:37

查看belongs_to关联的:touch参数:

:触摸

如果为 true,则关联的对象将是
触及(updated_at/on 属性
设置为现在)当该记录是
保存或销毁。如果您指定一个
符号,该属性将被更新
使用当前时间而不是
Updated_at/on 属性。

http://api.rubyonrails.org/classes/ActiveRecord /Associations/ClassMethods.html#method-i-belongs_to

因此,在您的 Comment 模型中,您将拥有:

belongs_to :photo, :touch => :comments_updated_at

现在,为了创建包含最近更新的评论的照片时间线,您需要做的就是

Photo.order('comments_updated_at DESC').all 

:请务必将“comments_updated_at”日期时间字段添加到您的照片模型中。

有道理吗?

Check out the :touch parameter of of the belongs_to association:

:touch

If true, the associated object will be
touched (the updated_at/on attributes
set to now) when this record is either
saved or destroyed. If you specify a
symbol, that attribute will be updated
with the current time instead of the
updated_at/on attribute.

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to

In your Comment model, therefore, you would have:

belongs_to :photo, :touch => :comments_updated_at

Now, in order to create a time line of photos with recently updated comments all you need to do is:

Photo.order('comments_updated_at DESC').all 

Just be sure to add the "comments_updated_at" datetime field to your Photo model.

Make sense?

爱的那么颓废 2024-11-13 10:29:37

对于这个问题的未来读者,SQlite vs Postgresql 中 SQL 问题的真正答案是,在 SQL“标准”中,每个选定的列都需要位于 GROUP BY 中或者是聚合功能。

https://www.techonthenet.com/sql/group_by.php (或其他您想要查看的 SQL 参考)

您的 SQLite 查询使用了 SELECT * 而不是特定列。在 Postgresql(MySQL、Maria,可能还有 MSSQL Server)等大多数数据库上,这会因类似的错误而崩溃。出于很多充分的理由,这绝对是无效的 SQL 语法。

在幕后,我不知道 SQlite 做了什么——也许它将 * 扩展为字段并将它们添加到幕后的 GROUP BY 中?但这不是一个好的 SQL 语句,这就是它引发错误的原因。

Just for the future readers of this question, the real answer to your SQL issue in SQlite vs Postgresql is that in the SQL "standard", every selected column needs to be in the GROUP BY or be an aggregate function.

https://www.techonthenet.com/sql/group_by.php (or whatever SQL ref you want to take a look at)

Your SQLite query used SELECT * instead of specific columns. That would have blown up with a similar error on most databases like Postgresql (MySQL, Maria, probably MSSQL Server). It's definitely invalid SQL grammar for a lot of good reasons.

Under the hood, I have no clue what SQlite does -- maybe it expands the * into fields and adds them to the GROUP BY under the hood? But its not a good SQL statement which is which it threw the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文