module MxCommentable
  extend ActiveSupport::Concern

  included do
    after_save :save_comment
    has_one :mx_comment, as: :mx_commentable, dependent: :destroy
  end

  def comment
    mx_comment.try(:comment)
  end

  def comment=(comment)
    build_mx_comment unless mx_comment
    mx_comment.comment = comment
  end

  private

  def save_comment
    if mx_comment.try(:comment).presence
      mx_comment.save
    else
      mx_comment.try(:destroy)
    end
  end
end

module MxCommentable
  extend ActiveSupport::Concern

  included do
    after_save :save_comment
    has_one :mx_comment, as: :mx_commentable, dependent: :destroy
  end

  def comment
    @comment || mx_comment.try(:comment)
  end

  def comment=(comment)
    @comment = comment
  end

  private

  def save_comment
    if @comment.presence
      if mx_comment
        mx_comment.update_attributes!(comment: @comment)
      else
        build_mx_comment(comment: @comment).save
      end
    else
      mx_comment.destroy if mx_comment
    end
  end
end

Fix insert before destroy on blank comment saving
Fix insert before destroy on blank comment saving
