millions-exabyte-27721
03/03/2025, 12:30 PMrb
class Surveys::Response < ApplicationRecord
belongs_to :user
belongs_to :surveys_question, class_name: "Surveys::Question"
has_and_belongs_to_many :surveys_question_options,
inverse_of: :surveys_response,
class_name: "Surveys::QuestionOption",
foreign_key: :surveys_response_id,
association_foreign_key: :surveys_question_option_id
end
rb
class Avo::Resources::SurveysSingleChoiceResponse < Avo::BaseResource
# self.includes = []
# self.attachments = []
self.model_class = ::Surveys::SingleChoiceResponse
# self.search = {
# query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) }
# }
def fields
field :id, as: :id
field :user, as: :belongs_to, readonly: true
field :surveys_question, as: :belongs_to, readonly: true
field :surveys_question_options, as: :has_and_belongs_to_many, readonly: true
end
endloud-jewelry-99127
03/03/2025, 12:31 PMloud-jewelry-99127
03/03/2025, 12:32 PMinverse_of ?loud-jewelry-99127
03/03/2025, 12:32 PMSurveys::QuestionOption?millions-exabyte-27721
03/03/2025, 12:32 PMAvo::AssociationsController
rb
def set_reflection
@reflection = @record.class.reflect_on_association(association_from_params)
# Ensure inverse_of is present on STI
if !@record.class.descends_from_active_record? && @reflection.inverse_of.blank? && Rails.env.development?
raise "Avo relies on the 'inverse_of' option to establish the inverse association and perform some specific logic.\n" \
"Please configure the 'inverse_of' option for the '#{@reflection.macro} :#{@reflection.name}' association " \
"in the '#{@reflection.active_record.name}' model."
end
endmillions-exabyte-27721
03/03/2025, 12:32 PMmillions-exabyte-27721
03/03/2025, 12:33 PMmillions-exabyte-27721
03/03/2025, 12:33 PMmillions-exabyte-27721
03/03/2025, 12:35 PMirb(#<Avo::AssociationsController...):001> @reflection
=> |
web | #<ActiveRecord::Reflection::HasAndBelongsToManyReflection:0x00000003018de9c0
web | @active_record=Surveys::Response(id: integer, user_id: integer, surveys_question_id: integer, type: string, answer: text, created_at: datetime, updated_at: datetime),
web | @association_foreign_key=nil,
web | @association_primary_key=nil,
web | @class_name="Surveys::QuestionOption",
web | @counter_cache_column=nil,
web | @foreign_key=nil,
web | @inverse_of=nil,
web | @inverse_which_updates_counter_cache=nil,
web | @inverse_which_updates_counter_cache_defined=false,
web | @join_table=nil,
web | @klass=Surveys::QuestionOption(id: integer, answer: string, surveys_question_id: integer, created_at: datetime, updated_at: datetime, position: integer),
web | @name=:surveys_question_options,
web | @options=
web | {:inverse_of=>:surveys_response, :class_name=>"Surveys::QuestionOption", :foreign_key=>:surveys_response_id, :association_foreign_key=>:surveys_question_option_id},
web | @plural_name="surveys_question_options",
web | @scope=nil>millions-exabyte-27721
03/03/2025, 12:35 PMloud-jewelry-99127
03/03/2025, 12:36 PMSurveys::SingleChoiceResponse model associations?millions-exabyte-27721
03/03/2025, 12:36 PMloud-jewelry-99127
03/03/2025, 12:36 PMmillions-exabyte-27721
03/03/2025, 12:36 PMmillions-exabyte-27721
03/03/2025, 12:37 PMrb
class Surveys::Response < ApplicationRecord
belongs_to :user
belongs_to :surveys_question, class_name: "Surveys::Question"
has_and_belongs_to_many :surveys_question_options,
inverse_of: :surveys_response,
class_name: "Surveys::QuestionOption",
foreign_key: :surveys_response_id,
association_foreign_key: :surveys_question_option_id
endloud-jewelry-99127
03/03/2025, 12:38 PMloud-jewelry-99127
03/03/2025, 12:38 PMSurveys::SingleChoiceResponse inherit from Surveys::Responsemillions-exabyte-27721
03/03/2025, 12:39 PMloud-jewelry-99127
03/03/2025, 12:39 PMSurveys::QuestionOption=millions-exabyte-27721
03/03/2025, 12:39 PMrb
Surveys::SingleChoiceResponse.first.surveys_question_options
Surveys::SingleChoiceResponse Load (0.4ms) SELECT "surveys_responses".* FROM "surveys_responses" WHERE "surveys_responses"."type" = 'Surveys::SingleChoiceResponse' ORDER BY "surveys_responses"."id" ASC LIMIT 1 /*application='FsgWebportal'*/
Surveys::QuestionOption Load (1.7ms) SELECT "surveys_question_options".* FROM "surveys_question_options" INNER JOIN "surveys_question_options_responses" ON "surveys_question_options"."id" = "surveys_question_options_responses"."surveys_question_option_id" WHERE "surveys_question_options_responses"."surveys_response_id" = 5 /* loading for pp */ LIMIT 11 /*application='FsgWebportal'*/
=>
[#<Surveys::QuestionOption:0x00000001256171e8
id: 7,
answer: "ĆVP",
surveys_question_id: 12,
created_at: "2025-03-03 10:06:22.495005000 +0000",
updated_at: "2025-03-03 10:06:22.495005000 +0000",
position: 2>]millions-exabyte-27721
03/03/2025, 12:40 PMrb
class Surveys::QuestionOption < ApplicationRecord
belongs_to :surveys_question, class_name: "Surveys::Question"
has_and_belongs_to_many :surveys_responses,
inverse_of: :surveys_question_option,
class_name: "Surveys::Response",
foreign_key: :surveys_question_option_id,
association_foreign_key: :surveys_response_id
acts_as_list scope: :surveys_question
endmillions-exabyte-27721
03/03/2025, 12:40 PMrb
Surveys::QuestionOption.find(7).surveys_responses
Surveys::QuestionOption Load (11.5ms) SELECT "surveys_question_options".* FROM "surveys_question_options" WHERE "surveys_question_options"."id" = 7 LIMIT 1 /*application='FsgWebportal'*/
Surveys::Response Load (0.6ms) SELECT "surveys_responses".* FROM "surveys_responses" INNER JOIN "surveys_question_options_responses" ON "surveys_responses"."id" = "surveys_question_options_responses"."surveys_response_id" WHERE "surveys_question_options_responses"."surveys_question_option_id" = 7 /* loading for pp */ LIMIT 11 /*application='FsgWebportal'*/
=>
[#<Surveys::SingleChoiceResponse:0x00000001277f8488
id: 5,
user_id: 36,
surveys_question_id: 12,
type: "Surveys::SingleChoiceResponse",
answer: nil,
created_at: "2025-03-03 12:15:17.986980000 +0000",
updated_at: "2025-03-03 12:15:17.986980000 +0000">]loud-jewelry-99127
03/03/2025, 12:40 PMloud-jewelry-99127
03/03/2025, 12:41 PMloud-jewelry-99127
03/03/2025, 12:41 PMloud-jewelry-99127
03/03/2025, 12:42 PMhas_and_belongs_to_many :surveys_responses, inverse_of: :surveys_question_options
instead
has_and_belongs_to_many :surveys_responses, inverse_of: :surveys_question_optionloud-jewelry-99127
03/03/2025, 12:42 PMmillions-exabyte-27721
03/03/2025, 12:43 PMloud-jewelry-99127
03/03/2025, 12:43 PMhas_and_belongs_to_many :surveys_question_options,
inverse_of: :surveys_responses
Instead
has_and_belongs_to_many :surveys_question_options,
inverse_of: :surveys_responsemillions-exabyte-27721
03/03/2025, 12:43 PMmillions-exabyte-27721
03/03/2025, 12:43 PMinverse_of for HABTM seems to be largely undocumentedmillions-exabyte-27721
03/03/2025, 12:43 PMloud-jewelry-99127
03/03/2025, 12:44 PMmillions-exabyte-27721
03/03/2025, 12:44 PMloud-jewelry-99127
03/03/2025, 12:45 PMmillions-exabyte-27721
03/03/2025, 12:46 PMloud-jewelry-99127
03/03/2025, 12:47 PMmillions-exabyte-27721
03/03/2025, 12:49 PMloud-jewelry-99127
03/03/2025, 12:52 PMloud-jewelry-99127
03/03/2025, 12:53 PMloud-jewelry-99127
03/03/2025, 12:53 PMmillions-exabyte-27721
03/03/2025, 12:57 PMmillions-exabyte-27721
03/03/2025, 12:58 PMloud-jewelry-99127
03/03/2025, 12:59 PMloud-jewelry-99127
03/03/2025, 12:59 PM