2014-12-03 2 views
5

이것은 ActiveAdmin 0.4.3과 동일합니다. 우리의 응용 프로그램은 Survey를 실행합니다. Survey에는 임의의 수의 SurveyQuestions가있을 수 있습니다. 사용자가 설문 조사를 채우면 UserSurveyComment 인스턴스가 생성되고 설문 조사의 설문 조사 요청마다 하나씩 has_many 설문 조사서가 작성됩니다.ActiveAdmin에서 동적 열 수 내보내기 CSV

이의 결과는 주어진 설문 조사에 대한 모든 UserSurveyComment 인스턴스가 SurveyComments 같은 수있을 것이라는 점을, 그러나 설문 조사 사이에이 숫자는 다를 수 있습니다.

ActiveAdmin CSV 내보내기에서 UserSurveyComments를 이러한 방식으로 처리하여 사용자, 설문 조사 및 각 설문 조사 항목에 대한 열이 차례로 나올 수 있습니까? 내보내기는 조사에 의해 범위가 지정되므로 각 행의 열은 같지만 특정 내보내기의 수는 다를 수 있습니다. 내가하고 싶은 무엇

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

같은 것입니다 ...하지만 ActiveAdmin.CSVBuilder 인스턴스 내에서 설문 조사에 도달 할 수있는 방법이있을 나타나지 않습니다.

는 아마 나를 그냥 내 자신의 컨트롤러에서이 작업을 수행하기가 쉽다?

+0

나는 개방이 질문을 떠날거야, 그러나 결국 나는 내 자신의 컨트롤러에서 사용자 지정 CSV 수출을했고, ActiveAdmin을을 떠났다. – pjmorse

답변

2

나는 @collection 출력을위한 필터링 된 개체의 목록을 포함, 모델이 csv 블록 내부

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 

에 유사한 것으로 이해합니다. 구성에서 다음과 같이 비슷한 방법으로 UserSurveyComment을 등록 할 수 있습니다

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