简体   繁体   English

如何在rails上的ruby中返回当前线程的所有未删除的消息?

[英]How do I return all non deleted messages of a current thread in ruby on rails?

Having a little trouble figuring out a way to display messages for users message threads depending on which ones have been deleted and not. 找出一种方法来显示消息给用户,这取决于是否删除了消息线程,这有点麻烦。 Deleted messages "sender_status or recipient_status" will change from 0 to 1 on deletion of a message. 删除消息后,已删除的消息“ sender_status或收件人_status”将从0更改为1。 Currently to display a list of users message threads in their inbox I use: 当前,为了在用户的收件箱中显示用户消息线程的列表,我使用:

eg 例如

current_user = User.find(2)
current_user.message_threads

This grabs all their message_threads with 0 sender or recipient statuses. 这将捕获所有具有0个发件人或收件人状态的message_threads。

class User < ActiveRecord::Base

  has_many :messages
  has_many :message_threads

Nice and easy but what I would like to do now is some how set up something that enables me to be able to grab messages with 0 sender or recipient statuses from a current thread. 不错,很容易,但是我现在想做的是如何设置一些东西,使我能够从当前线程中以发送者或接收者状态为0捕获消息。 So if I was to type: 所以,如果我要输入:

one_thread = current_user.message_threads.first

From this thread I'd want to be able to easily grab the only messages I needed. 从这个线程,我希望能够轻松地获取我所需的仅有消息。 Would like to put something in the message_thread model so I could eventually type out: 想在message_thread模型中放置一些内容,以便最终输入:

current_user.message_threads.first.messages

#or

one_thread.messages

and have only the messages I needed loaded and the ones with "1" statuses ignored. 并且仅加载我需要的消息,而忽略状态为“ 1”的消息。

User Model: 用户模型:

class User < ActiveRecord::Base

  has_many :messages
  has_many :message_threads

  def message_threads
    MessageThread.where([ '(sender_id = ? AND sender_status = 0) OR (recipient_id = ? AND recipient_status = 0)', self.id, self.id ])
  end

  def messages
    Message.where([ '(sender_id = ? AND sender_status = 0) OR (recipient_id = ? AND recipient_status = 0)', self.id, self.id ])
  end

MessageThread model: MessageThread模型:

class MessageThread < ActiveRecord::Base

  has_many :messages

Message model: 消息模型:

  class Message < ActiveRecord::Base

    acts_as_tree
    has_one :message_thread

I tried experimenting with joins but this didn't work out well for me. 我尝试尝试加入,但这对我来说效果不佳。

def messages
  joins(:messages).merge(  Message.where([ '(parent_id = ? AND sender_status = 0) OR (parent_id = ? AND recipient_status = 0)',self.message_id, self.message_id ]) )
end

Help would be much appreciate. 帮助将不胜感激。

Kind regards 亲切的问候

I'm guessing the problem is that one_thread is returning a MessageThread object, and then you're trying to get the user id by calling self.id , but it's being called on the wrong model. 我猜测问题是one_thread返回一个MessageThread对象,然后您试图通过调用self.id来获取用户ID,但是在错误的模型上调用了它。

I haven't tried this code but it might work: 我没有尝试过此代码,但它可能有效:

class User < ActiveRecord::Base
  def self.threads_in_inbox
    message_threads.where('(sender_id = ? AND sender_status = 0) OR (recipient_id = ? AND recipient_status = 0)', self.id, self.id)
  end
end

class MessageThread < ActiveRecord::Base
  def self.inbox_messages
    messages.where('(sender_id = ? AND sender_status = 0) OR (recipient_id = ? AND recipient_status = 0)', self.user.id, self.user.id)
    # this hits the database 1 extra time however due to self.user.id
    # you could change it to inbox_messages(user_id) and pass in the id manually
  end
end

first_thread = current_user.threads_in_inbox.first
first_thread.inbox_messages

In your question it seems like you're overriding the association queries generated by rails (message_threads and messages) - not sure if it's on purpose but I changed it above. 在您的问题中,似乎您要覆盖由rails(message_threads和message)生成的关联查询-不确定它是否是故意的,但我在上面进行了更改。

Also, I think you can remove the duplication and do the following: 另外,我认为您可以删除重复项并执行以下操作:

where('(sender_id = ?) AND (sender_status = 0 OR recipient_status = 0)', self.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM