对消息进行分组以显示最新的概览
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ActiveRecord 时要熟悉的最重要的事情是查看输出日志。如果您在本地主机上运行应用程序,由
rails server
启动,那么您应该会看到所有 SQL 输出到控制台中。我说“检查你的 SQL”是因为你的方法不太有意义。如果您只想选择特定对话并检索其最后一条消息,那么您应该执行以下操作:
如果您尝试获取每个
Listing
的最后一条Message
,那么你需要从不同的角度工作,如下所示: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:
If you're trying to get the last
Message
for eachListing
, then you need to work from a different perspective, like so: