简体   繁体   English

如何获取用户在主题中创建的最后评论的日期

[英]How to get the date of the last comment a user created in a topic

I have a Topic model, Comment Model and a User model. 我有一个主题模型,评论模型和一个用户模型。 They each have associations with each other. 他们彼此之间有联系。 I want to see the date of the last comment that was commented in a uniq topic. 我想查看在uniq主题中被评论的最后评论的日期。 I was thinking something like this would work but it just shows the last comment created by a user and not for each topic. 我在想这样的事情会起作用,但是它只是显示用户创建的最后评论,而不是针对每个主题。

  = topic.user.comments.last.created_at.strftime("%b, %d, %Y")

  %table.table
            %thead
                %tr
                    %th Topic
                    %th Users
                    %th Replies
                    %th Activity
            - @topics.each do |topic|
                %tbody
                    %tr
                        %td{:width => "800"}
                            = link_to topic.title, topic_path(topic)
                        %td{:width => "200"}
                            .users-image
                                - topic.comments.uniq(&:user_id).each do |comment|
                                    = image_tag(comment.user.avatar.url(:thumb))
                        %td{:width => "80"}
                            .comments
                                = topic.comments.count
                        %td{:width => "80"}
                            .activity
                                =# topic.user.comments.last.created_at.strftime("%b, %d, %Y")

Comments table 评论表

create_table "comments", force: :cascade do |t|
 t.string   "commentable_type"
 t.integer  "commentable_id"
 t.integer  "user_id"
 t.text     "body"
 t.datetime "created_at",       null: false
 t.datetime "updated_at",       null: false
end

Just use 只需使用

topic.comments.last.created_at.strftime("%b, %d, %Y")

will get the last comment date of the topic. 将获得该主题的最后评论日期。

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

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