Я работаю над приложением, в котором пользователь может отправлять различные опросы.Каждый опрос имеет несколько вопросов.У меня есть сценарий, в котором я хочу запретить пользователю отправлять каждый опрос только один раз. Это означает, что если пользователь отправляет опрос, и он приходит снова, чтобы отправить тот же опрос, ему не должно быть разрешено повторять этот опрос, и должно отображаться сообщение об ошибке.'Вы уже отправили этот опрос'.
Модель пользователя
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :role
has_many :survey_answers
def full_name
full_name = "#{self.first_name} #{self.last_name}".strip
full_name.present? ? full_name : self.user_name
end
end
Модель опроса
class Survey < ApplicationRecord
has_attached_file :logo
do_not_validate_attachment_file_type :logo
has_many :questions, -> { order(position: :asc) } , :dependent =>
:destroy
accepts_nested_attributes_for :questions, allow_destroy: true
has_many :survey_answers
end
Модель вопроса
class Question < ApplicationRecord
belongs_to :survey, optional: true
acts_as_list scope: :survey
has_many :question_options, :dependent => :destroy
accepts_nested_attributes_for :question_options, allow_destroy:
true, reject_if: proc { |attributes| attributes['title'].blank? }
has_many :survey_answers
end
Ответы на опросмодель
class SurveyAnswer < ApplicationRecord
belongs_to :question, optional: true
belongs_to :user, optional: true
belongs_to :survey, optional: true
end