鉴于以下模型:
Thread (id, title, timestamps)
ThreadParticipant (id, thread_id, user_id, read(boolean), timestamps)
Comment (id, thread_id, user_id)
如何在返回的地方构造查询:最近的线程注释(Thread.comment.first)等于current_user的所有线程?
...返回最后一个comment.user_id == current_user.id?
的线程想法?
答案 0 :(得分:1)
尝试类似[未经测试的代码]:
Thread.find_by_sql(["Select * from Threads where ((select user_id from comments where threads.id = comments.thread_id ORDER by created_at DESC LIMIT 1) = ?)", current_user.id ])
或尝试一下(不确定是否要求不同会自动选择第一个,但我猜它应该):
Thread.find(:all, :joins => :comments , :select => ["distinct threads.id"], :conditions => ["comments.user_id = ?", current_user.id])