2014-10-27 22:36:38 -07:00
|
|
|
class Question < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
2014-12-28 12:42:21 -08:00
|
|
|
has_many :answers, dependent: :destroy
|
2014-12-27 15:34:56 -08:00
|
|
|
has_many :inboxes, dependent: :destroy
|
2014-12-08 06:34:37 -08:00
|
|
|
|
2014-12-08 14:02:31 -08:00
|
|
|
validates :content, length: { maximum: 255 }
|
2014-12-14 03:03:57 -08:00
|
|
|
|
2014-12-28 12:58:11 -08:00
|
|
|
before_destroy do
|
2015-04-29 17:22:24 -07:00
|
|
|
rep = Report.where(target_id: self.id, type: 'Reports::Question')
|
2015-04-29 17:04:43 -07:00
|
|
|
rep.each do |r|
|
|
|
|
unless r.nil?
|
|
|
|
r.deleted = true
|
|
|
|
r.save
|
|
|
|
end
|
2015-04-28 01:46:02 -07:00
|
|
|
end
|
2015-04-29 17:04:43 -07:00
|
|
|
|
2014-12-28 12:58:11 -08:00
|
|
|
user.decrement! :asked_count unless self.author_is_anonymous
|
|
|
|
end
|
|
|
|
|
2014-12-14 03:19:52 -08:00
|
|
|
def can_be_removed?
|
|
|
|
return false if self.answers.count > 0
|
|
|
|
return false if Inbox.where(question: self).count > 1
|
|
|
|
true
|
2014-12-14 03:03:57 -08:00
|
|
|
end
|
2014-10-27 22:36:38 -07:00
|
|
|
end
|