对消息进行分组以显示最新的概览

发布于 2024-10-30 22:15:06 字数 422 浏览 1 评论 0原文

我正在尝试在 Rails 3 应用程序中以有组织的方式显示消息。我正在尝试按列表 ID 对消息进行分组,并显示属于该列表的对话中的最新消息。

我正在我的控制器中尝试此语法:

@messages = Message.all(:conditions => { :recipient_id => current_user.id },
                        :group => :listing_id,
                        :order => "created_at DESC")

这通过列表成功地将我的消息分组在一起,但是它不显示该组中的最后一条消息。它显示了最古老的一个。

有谁知道如何做到这一点?我尝试过使用 :having 子句,但到目前为止没有效果。

I'm trying to show messages in an organized way in my rails 3 app. I'm trying to group the messages by a listing id and showing the latest message in that conversation belonging to the listing.

I'm trying this syntax in my controller:

@messages = Message.all(:conditions => { :recipient_id => current_user.id },
                        :group => :listing_id,
                        :order => "created_at DESC")

This successfully groups together my messages by listing, however it doesn't show the last message within that group. It shows the oldest one.

Does anyone know how to accomplish this? I've tried using the :having clause, but to no avail so far.

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

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

发布评论

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

评论(1

清风无影 2024-11-06 22:15:06

使用 ActiveRecord 时要熟悉的最重要的事情是查看输出日志。如果您在本地主机上运行应用程序,由 rails server 启动,那么您应该会看到所有 SQL 输出到控制台中。

我说“检查你的 SQL”是因为你的方法不太有意义。如果您只想选择特定对话并检索其最后一条消息,那么您应该执行以下操作:

@messages = Message.all(:conditions => 
                      { :recipient_id => current_user.id,
                        :listing_id => params[:listing_id] },
                        :order => "created_at DESC")

如果您尝试获取每个 Listing 的最后一条 Message ,那么你需要从不同的角度工作,如下所示:

@listings = Listings.all(:conditions => {:recipient_id => current_user.id},
                         :include => :messages,
                         :order => 'messages.created_at DESC')

last_messages_of_first_listing = @listings.first.messages.first

The most important thing to get familiar with when using ActiveRecord, is watching your output logs. If you're running your app off localhost, started by rails server, then you should be seeing all SQL outputted into your console.

I say 'check your SQL' because your approach doesn't quite make sense. If you want to only select a specific conversation and retrieve its last message, then you should doing something like this:

@messages = Message.all(:conditions => 
                      { :recipient_id => current_user.id,
                        :listing_id => params[:listing_id] },
                        :order => "created_at DESC")

If you're trying to get the last Message for each Listing, then you need to work from a different perspective, like so:

@listings = Listings.all(:conditions => {:recipient_id => current_user.id},
                         :include => :messages,
                         :order => 'messages.created_at DESC')

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