【问题标题】:How to show commenting user's id in notifications?如何在通知中显示评论用户的 ID?
【发布时间】:2015-08-27 17:25:51
【问题描述】:

在朋友的帮助下,我得到了正确设置 CommentNotification 的 user_id:

[2] pry(main)> Comment.find(8)
 id: 8,
 content: "test",
 goal_id: nil,
 habit_id: nil,
 valuation_id: 9,
 quantified_id: nil,
 commentable_id: nil,
 commentable_type: nil,
 user_id: 1,
 created_at: Thu, 11 Jun 2015 19:57:26 UTC +00:00,
 updated_at: Thu, 11 Jun 2015 19:57:26 UTC +00:00,
 likes: nil>
[3] pry(main)> Comment.find(9)
 id: 9,
 content: "test",
 goal_id: nil,
 habit_id: nil,
 valuation_id: 2,
 quantified_id: nil,
 commentable_id: nil,
 commentable_type: nil,
 user_id: 2,
 created_at: Thu, 11 Jun 2015 19:57:55 UTC +00:00,
 updated_at: Thu, 11 Jun 2015 19:57:55 UTC +00:00,
 likes: nil>
[4] pry(main)> Notification.find(8)
 id: 8,
 habit_id: nil,
 quantified_id: nil,
 valuation_id: 9,
 goal_id: nil,
 comment_id: 8,
 user_id: 1,
 habit_like_id: nil,
 quantified_like_id: nil,
 valuation_like_id: nil,
 goal_like_id: nil,
 followed_id: nil,
 comment_like_id: nil,
 read: true,
 created_at: Thu, 11 Jun 2015 19:57:26 UTC +00:00,
 updated_at: Thu, 11 Jun 2015 19:57:29 UTC +00:00>
[5] pry(main)> Notification.find(10)
 id: 10,
 habit_id: nil,
 quantified_id: nil,
 valuation_id: 3,
 goal_id: nil,
 comment_id: 10,
 user_id: 2,
 habit_like_id: nil,
 quantified_like_id: nil,
 valuation_like_id: nil,
 goal_like_id: nil,
 followed_id: nil,
 comment_like_id: nil,
 read: true,
 created_at: Thu, 11 Jun 2015 20:00:14 UTC +00:00,
 updated_at: Thu, 11 Jun 2015 20:00:30 UTC +00:00>

但现在user_id 仍然没有在部分中显示正确的 id。尽管rails console 清楚地显示有人说 2,但所有通知仍然显示“1 评论了您的价值”。

notifications_controller

class NotificationsController < ApplicationController
  before_action :correct_user, only: [:destroy]

  def index
    @notifications = current_user.notifications
    @notifications.each do |notification|
      notification.update_attribute(:read, true) 
    end
  end

comment.rb

class Comment < ActiveRecord::Base
    after_create :create_notification
  has_many :notifications
  has_many :comment_likes   
  has_many :likers, through: :comment_likes, class_name: 'User', source: :liker
  belongs_to :habit
  belongs_to :quantified
  belongs_to :valuation
  belongs_to :goal
    belongs_to :user
  validates :user, presence: true

private

  def create_notification
    @valuation = Valuation.find_by(self.valuation_id)
    @user = User.find_by(@valuation.user_id).id
      self.notifications.create(
        valuation: self.valuation,
        user: self.valuation.user,
        read: false
      )
  end
end

通知/_notifications

<%= link_to Comment.find_by(notification.user_id).user.id, user_path(Comment.find_by(notification.comment_id).user.id) %> commented on <%= link_to "your value", notification_valuation_path(notification, notification.valuation_id) %>

【问题讨论】:

  • 为什么在每次页面加载时更新所有通知?而且您不应该在视图层上使用数据库。您展示了您的通知模板,但您如何遍历通知?

标签: ruby-on-rails ruby model-view-controller notifications


【解决方案1】:

如果您包含notification.rb,将会有所帮助。 comment.user.idcomment.user_id 相等(但性能较差),如果评论 belongs_to 或 has_one :user。您不应该在视图中查看数据库,我认为 Comment.find_by(1) 甚至不起作用,请查看您的基本 Rails 概念。

但是回答你原来的问题。 link_to 的第一个参数是显示的文本,所以... .user.id 显示的是用户的 id 而不是用户名。

【讨论】:

  • 这个问题的字面意思是“如何在通知中显示评论用户的姓名?”并且他看到“1 评论了您的价值”。这仅仅是因为他显示的是.id 而不是.name
  • Leito 我更准确地将标题名称反映为明确地说id,但即使仍将其更改为.name,也不会神奇地显示正确的评论用户名。 Notification.rb 有belongs_to :valuation:comment
  • 这并不神奇”,.name 将调用该对象的 name 方法。你看到了什么?你必须在这里帮助我们,给我们更多细节!也简化你的答案,开始采用一些所有答案中给出的改进代码的建议。
【解决方案2】:

Comment.find_by(notification.user_id) 是如何工作的?

我认为正确的方法是(第一和第二是等价的):

  • Comment.find_by_id(notification.user_id)source
  • Comment.find_by(id: notification.user_id)
  • Comment.find(id: notification.user_id)source

如果你只需要 user_id 为什么不写这个: notification.user_id 而不是 Comment.find_by(notification.user_id).user.id

【讨论】:

  • 啊,你是对的notification.user_id 确实有效,问题是通知都被设置为:user_id 的用户,其估值不是它的评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
  • 2015-12-19
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2010-11-06
相关资源
最近更新 更多