关于我最后一次尝试的答案,我可以实现自己的目标通缉。应用正常运行后,我发现情况有所不同:仅显示创建消息并发送消息的人。如果我回复,则看不到回复,我知道问题所在。
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) *')更多推荐
显示消息数组中的最后一条消息
发布评论