如何在通知中显示评论用户的 ID?
How to show commenting user's id in 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。尽管 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/_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) %>
如果您包含 notification.rb
将会有所帮助。如果评论 belongs_to 或 has_one :user,comment.user.id
等于(但性能较低)comment.user_id
。您不应该在视图中查找数据库,我认为 Comment.find_by(1)
甚至没有用请复习您的基本 Rails 概念。
但是回答你原来的问题。 link_to
的第一个参数是显示的文本,因此 ... .user.id
显示用户的 id 而不是用户名。
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
在我朋友的一点帮助下,我得到 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。尽管 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/_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) %>
如果您包含 notification.rb
将会有所帮助。如果评论 belongs_to 或 has_one :user,comment.user.id
等于(但性能较低)comment.user_id
。您不应该在视图中查找数据库,我认为 Comment.find_by(1)
甚至没有用请复习您的基本 Rails 概念。
但是回答你原来的问题。 link_to
的第一个参数是显示的文本,因此 ... .user.id
显示用户的 id 而不是用户名。
Comment.find_by(notification.user_id)
是如何工作的?
我认为正确的方法是(第一个和第二个是等价的):
Comment.find_by_id(notification.user_id)
sourceComment.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