undefined method `destroy' on Public Activity

前端 未结 1 989
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 16:25

User\'s can comment on a Screen and it\'s tracked by PublicActivity :

@comment.create_activity :create, owner: current_user, recipient: @comment.screen.user
         


        
1条回答
  •  借酒劲吻你
    2021-01-03 16:59

    This isn't tested, but this is what I think you should do.

    First you can remove the references to activities in your screens_controller#destroy

    Then in your comments_controller#destroy

      @comment = current_user.comments.find(params[:id])
      @activity = PublicActivity::Activity.find_by(trackable_id: (params[:id]), trackable_type: controller_path.classify)
      @activity.destroy
      @comment.destroy
    

    Should be outside of your respond to block

    Next in your comments model you should do something like this:

    #comment.rb
    
    private
    
    before_destroy :find_and_destroy_comments
    
    def find_and_destroy_comments
      activity = PublicActivity::Activity.find_by(trackable_id: self.id, trackable_type: self.class.name)
      if activity.present?
        activity.destroy
      end
    end
    

    calling the before_destroy method overrides the default ruby destroy method that is called during dependent: :destroy

    Let me know if this works, but It should.

    0 讨论(0)
提交回复
热议问题