显示消息数组中的最后一条消息

编程入门 行业动态 更新时间:2024-10-22 04:56:52
本文介绍了显示消息数组中的最后一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

关于我最后一次尝试的答案,我可以实现自己的目标通缉。应用正常运行后,我发现情况有所不同:仅显示创建消息并发送消息的人。如果我回复,则看不到回复,我知道问题所在。

Regarding my last attempted answer, I could achieve what I wanted. Once the app is working I noticed something is different: only the person that created the message and sends a message was only showing. If I reply, I do not see the reply and I know the problem.

我的消息模型具有表: [:id,:user_id,:to,:body,...]

:user_id 与来自的相似,因为它用于确定谁创建了邮件。

:user_id would be similar to from as that is used to determine who created the message.

用户1 是客户,用户2 是学生。学生向客户发送一条消息。只有我的客户端代码是这样的:

User 1 would be Client and User 2 is Student. Student sends a message to Client. Only my client side the code is as this:

#Controller Index: @messages = Message.where(to: current_user.id) # My replies are not included here .order(user_id: :asc, created_at: :desc) .select('distinct on (user_id) *')

我是说给我发给我的所有邮件(:to )id 1 。

I'm saying give me all messages that's addressed to me (:to) an id of 1.

我不会看到我的回复,因为:to 列。

I won't see my reply because of the :to column.

我的(客户)回复如下:

to: #student id user_id: #client id

和学生消息:

to: #client id user_id: #student id

因此,由于至现在指向一个id,因此说 2 我看不到答复。我可能需要更改模型,但是如果您看到一种简单的方法,请告诉我。

So because the to now point to an id, let's say 2 I wont see the reply. I may need to change up the model but if you see an easy way, please let me know.

编辑:

有效的答案

@messages = Message.where(to: current_user.id).or(Message.where(user_id: current_user.id)) .order(connection: :desc, created_at: :desc) .select('distinct on (connection) *')

推荐答案

由于您使用的是Rails 5,因此可以利用它的或方法:

Since you are using Rails 5, you can take advantage of it's or method:

Message.where(to: current_user.id).or( Message.where(user_id: current_user.id) ).order(connection: :desc, created_at: :desc) .select('distinct on (connection) *')

更多推荐

显示消息数组中的最后一条消息

本文发布于:2023-10-27 23:20:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   组中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!