Skip to content

Instantly share code, notes, and snippets.

@courtsimas
Created July 24, 2011 23:19
Show Gist options
  • Save courtsimas/1103228 to your computer and use it in GitHub Desktop.
Save courtsimas/1103228 to your computer and use it in GitHub Desktop.
def self.destroy_and_remove_events(params)
question = Question.find params[:question_id]
transaction do
answers_to_del, comments_to_del = [], []
answers = question.answers;
comments = []; answers.each {|a| comments << a.comments; answers_to_del << a.id}; comments.flatten!
comments.each{|c| comments_to_del = c.id}
TimelineEvent.delete_all(["subject_type = 'Answer' AND ( subject_id IN (?) )", answers_to_del.to_a.join(',')]) if answers_to_del.present?
TimelineEvent.delete_all(["subject_type = 'Comment' AND ( subject_id IN (?) )", comments_to_del.to_a.join(',')]) if comments_to_del.present?
TimelineEvent.delete_all(["subject_type = 'Question' AND ( subject_id = ? )", question.id])
question.destroy
true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment