Retrospring/app/models/inbox.rb

24 lines
584 B
Ruby

class Inbox < ActiveRecord::Base
belongs_to :user
belongs_to :question
def answer(answer, user)
answer = Answer.create!(content: answer,
user: user,
question: self.question)
user.increment! :answered_count
self.question.increment! :answer_count
self.destroy
answer
end
def remove
unless self.question.user.nil?
self.question.user.decrement! :asked_count if self.question.answer_count == 1
end
self.question.destroy if self.question.answer_count == 1
self.destroy
end
end