osem-fcy/app/models/answer.rb

22 lines
633 B
Ruby
Raw Normal View History

2014-02-09 18:05:57 +02:00
class Answer < ActiveRecord::Base
attr_accessible :title
2014-08-18 21:26:45 +03:00
has_many :qanswers, dependent: :destroy
2014-07-21 14:49:05 +02:00
has_many :questions, through: :qanswers
2014-02-09 18:05:57 +02:00
2014-07-21 14:49:05 +02:00
validates :title, presence: true
validate :no_modification_if_used
def no_modification_if_used
errors.add('', 'cannot be altered or deleted, if they are being used.') if self.title_changed? && self.questions.any?
end
# Gets answer, question, conference
## Returns
# the amount of replies for a given answer
# + integer +
def sum_replies question, conference
self.qanswers.find_by(question: question).registrations.where(conference: conference).count
end
2014-02-09 18:05:57 +02:00
end