Export dynamic column count in ActiveAdmin CSV

This is with ActiveAdmin 0.4.3. Our app runs polls, which can have any number of SurveyQuestions. When a user fills out a survey, an instance of UserSurveyComment is created, which has_many SurveyComments, one for each Survey SurveyQuestions survey.

As a result of this, for any given survey, all instances of UserSurveyComment will have the same number of SurveyComments, but this number may vary between polls.

Is it possible to export ActiveSQL CSV to process UserSurveyComments so that in turn there are columns for the user, the survey, and then each survey? Export is covered by the Overview, so each row has the same columns, but a particular export may have a different number.

What I would like to do is something like

 survey.survey_questions.each do |sq| column "Question" { |q| q.survey_comments.where(survey_question_id: sq.id).first.submitted_text } end 

... but in the instance of ActiveAdmin.CSVBuilder, there seems to be no way to get to the Overview.

Maybe it’s just easier for me to do this in my own controller?

+5
source share
1 answer

I understand that your model is similar to

 class Survey < ActiveRecord::Base has_many :user_survey_comments has_many :survey_questions end class SurveyQuestion < ActiveRecord::Base attr_accessor :name belongs_to :survey has_many :survey_comments end class UserSurveyComments < ActiveRecord::Base belongs_to :survey has_many :survey_comments end class SurveyComments < ActiveRecord::Base attr_accessor :content belongs_to :user_survey_comments belongs_to :survey_question end 

Inside the csv @collection contains a list of objects filtered for output. In the configuration, you can register UserSurveyComment as follows:

 ActiveAdmin.register UserSurveyComment do csv do column(:survey) visited_surveys = Set[] @collection.each do |user_survey_comment| next if visited_surveys.include?(user_survey_comment.survey) visited_surveys.add(user_survey_comment.survey) user_survey_comment.survey.survey_questions do |question| column(question.name) do |user_survey_comment| user_survey_comment .survey_comments .find_by(survey_question_id=question.id) .try(:response){''} end end end end end 
+2
source

Source: https://habr.com/ru/post/1208271/


All Articles