如何在通知中显示评论用户的ID?

时间:2015-06-11 20:15:41

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

在朋友们的帮助下,我得到了user_id,可以正确设置Comment& Notification

[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。所有通知仍然会说“1评论您的价值”,即使rails console清楚地显示有人说2。

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) %>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

如果您加入notification.rb,这将有所帮助。如果注释belongs_to或has_one:user,则comment.user.idcomment.user_id相等(但效果较差)。您不应该在视图中查找数据库,我认为Comment.find_by(1)甚至不起作用,请查看基本的Rails概念。

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