如果我对自己的事情发表评论,我就不应该收到通知。我们如何删除这个"功能"?
class Comment < ActiveRecord::Base
after_save :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 :stat
belongs_to :valuation
belongs_to :goal
belongs_to :user
validates :user, presence: true
default_scope { order('created_at') }
private
def create_notification
author =
if goal
goal.user
#elsif comment_like
# comment_like.user
elsif habit
habit.user
elsif stat
stat.user
elsif valuation
valuation.user
end
notifications.create(
comment: self,
likes: likes,
habit: habit,
stat: stat,
goal: goal,
valuation: valuation,
user: author,
read: false
)
end
end
可以对目标,估值,统计数据和&amp;习惯。
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
def destroy
@notification = Notification.find(params[:id])
@notification.destroy
redirect_to :back
end
private
def correct_user
@notification = current_user.notifications.find_by(id: params[:id])
redirect_to root_url, notice: "Not authorized to edit this notification" if @notification.nil?
end
end
CommentsController
class CommentsController < ApplicationController
before_action :set_commentable, only: [:index, :new, :create]
before_action :set_comment, only: [:edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
def index
@comments = @commentable.comments
end
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(comment_params)
if @comment.persisted?
redirect_to @commentable, notice: "Comment created."
else
render :new
end
end
def edit
end
def update
if @comment.update_attributes(comment_params)
redirect_to :back, notice: "Comment was updated."
else
render :edit
end
end
def destroy
@comment.destroy
redirect_to @comment.commentable, notice: "Comment destroyed."
end
def like
@comment = Comment.find(params[:id])
@comment_like = current_user.comment_likes.build(comment: @comment)
if @comment_like.save
@comment.increment!(:likes)
flash[:success] = 'Thanks for liking!'
else
flash[:error] = 'Too many likes'
end
redirect_to(:back)
end
private
def set_commentable
@commentable = find_commentable
end
def set_comment
@comment = current_user.comments.find(params[:id])
end
def correct_user
@comment = current_user.comments.find_by(id: params[:id])
redirect_to root_url, notice: "Not authorized to edit this comment" if @comment.nil?
end
def find_commentable
if params[:goal_id]
Goal.find(params[:goal_id])
elsif params[:habit_id]
Habit.find(params[:habit_id])
elsif params[:valuation_id]
Valuation.find(params[:valuation_id])
elsif params[:stat_id]
Stat.find(params[:stat_id])
end
end
def comment_params
params[:comment][:user_id] = current_user.id
params.require(:comment).permit(:content, :commentable, :user_id, :like)
end
end
如果您需要进一步的代码或解释,请与我们联系。长寿&amp;繁荣:]
答案 0 :(得分:2)
理想情况下,您不应该依赖回调。特别是如果像这种情况一样,你的回调必须访问在模型上下文之外不可用的数据。
解决方案很简单。而不是直接在控制器中创建调用ActiveRecord方法的注释,而是在注释本身中定义自定义方法。
class Comment
# ...
def self.create_comment(user, attributes)
transaction do
comment = new(attributes)
if comment.save
create_notification(user)
# other after creation methods here
end
comment
end
end
end
现在create_notification
可以访问当前用户。然后,在你的控制器中:
def create
comment = Comment.create(current_user, params[:comments])
if comment.persisted?
# saved
else
# not saved
# comment.errors
end
end
有几种变化。您也可以始终使用实例。
class Comment
# ...
def create_comment(user, attributes)
transaction do
comment.attributes = attributes
if result = comment.save
create_notification(user)
# other after creation methods here
end
result
end
end
end
def create
comment = Comment.new
if comment.create_comment(current_user, params[:comments])
# saved
else
# not saved
# comment.errors
end
end
无论你的实现是什么,关键是回调不应该被滥用。特别是,它们应该用于保证同一模型内的一致性,并且您不应该开始在模型之间引入显式依赖关系,否则您的代码将很快变得缓慢且无法维护。