diff --git a/decidim-module-forms_locations/app/models/concerns/decidim/forms_locations/answer_choice_extensions.rb b/decidim-module-forms_locations/app/models/concerns/decidim/forms_locations/answer_choice_extensions.rb index 474ad8a..e157ff1 100644 --- a/decidim-module-forms_locations/app/models/concerns/decidim/forms_locations/answer_choice_extensions.rb +++ b/decidim-module-forms_locations/app/models/concerns/decidim/forms_locations/answer_choice_extensions.rb @@ -6,15 +6,24 @@ module AnswerChoiceExtensions extend ActiveSupport::Concern included do - belongs_to :location_option, - class_name: "LocationOption", - foreign_key: "decidim_location_option_id", - optional: true + # Define a module variable to track if the association has been defined + class_attribute :answer_option_association_defined + self.answer_option_association_defined = false unless self.answer_option_association_defined + + # Redefine the belongs_to :answer_option association if not already defined + unless self.answer_option_association_defined + belongs_to :answer_option, + class_name: "AnswerOption", + foreign_key: "decidim_answer_option_id", + optional: true - belongs_to :answer_option, - class_name: "AnswerOption", - foreign_key: "decidim_answer_option_id", - optional: true + self.answer_option_association_defined = true + end + + belongs_to :location_option, + class_name: "LocationOption", + foreign_key: "decidim_location_option_id", + optional: true end end end diff --git a/decidim-module-forms_locations/app/models/decidim/forms/answer_choice.rb b/decidim-module-forms_locations/app/models/decidim/forms/answer_choice.rb new file mode 100644 index 0000000..5b9a79b --- /dev/null +++ b/decidim-module-forms_locations/app/models/decidim/forms/answer_choice.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Decidim + module Forms + class AnswerChoice < Forms::ApplicationRecord + belongs_to :answer, + class_name: "Answer", + foreign_key: "decidim_answer_id" + + belongs_to :answer_option, + class_name: "AnswerOption", + foreign_key: "decidim_answer_option_id" + + belongs_to :matrix_row, + class_name: "QuestionMatrixRow", + foreign_key: "decidim_question_matrix_row_id", + optional: true + + validates :matrix_row, presence: true, if: -> { answer.question.matrix? } + end + end +end