app/models/plan.rb
# The most recent answer to the given question id optionally can create an answer if
# none exists.
#
# qid               - The id for the question to find the answer for
# create_if_missing - If true, will genereate a default answer
#                     to the question (defaults: true).
#
# Returns Answer
# Returns nil
# rubocop:disable Metrics/AbcSize, Style/OptionalBooleanParameter
The most recent answer to the given question id optionally can create an answer if
none exists.

qid               - The id for the question to find the answer for
create_if_missing - If true, will genereate a default answer
to the question (defaults: true).

Returns Answer
Returns nil
rubocop:disable Metrics/AbcSize, Style/OptionalBooleanParameter
The most recent answer to the given question id optionally can create an answer if none exists . qid - The id for the question to find the answer for create_if_missing - If true will genereate a default answer to the question ( defaults : true ) . Returns Answer Returns nil rubocop : disable Metrics / AbcSize Style / OptionalBooleanParameter
def answer(qid, create_if_missing = true)
    answer = answers.select { |a| a.question_id == qid }
                    .max { |a, b| a.created_at <=> b.created_at }
    if answer.nil? && create_if_missing
      question = Question.find(qid)
      answer = Answer.new
      answer.plan_id     = id
      answer.question_id = qid
      answer.text        = question.default_value
      default_options    = []
      question.question_options.each do |option|
        default_options << option if option.is_default
      end
      answer.question_options = default_options
    end
    answer
  end
def answer ( qid , create_if_missing = true ) answer = answers . select { | a | a . question_id == qid } . max { | a , b | a . created_at <=> b . created_at } if answer . nil? && create_if_missing question = Question . find ( qid ) answer = Answer . new answer . plan_id = id answer . question_id = qid answer . text = question . default_value default_options = [ ] question . question_options . each do | option | default_options << option if option . is_default end answer . question_options = default_options end answer end
The most recent answer to the given question id optionally can create an answer if
none exists.
The most recent answer to the given question id optionally can create an answer if none exists .
qid
create_if_missing