I believe form (1) should certainly work after removing the line
‘has_many :survey_questions, :through => :surveys’
I am uncertain if form (2) will work (because I am uncertain if
has_many :through works that way and no
time to test it here)
I don’t see how ‘@company.survey_answers’ could work since there is no
has_many
relationship with the name ‘survey_answers’ in the Company class. It
is only defined
in the SurveyQuestions class.
As you say @company.surveys returns an array of Surveys, so normally you
would have to say @company.surveys[i].survey_questions which will again
return an array of questions, and so on. Are you asking for a means to
automatically combine all the results obtained by iterating each of the
arrays down the chain? I don’t know of a way to do that automatically,
without iterating each of the arrays and building a combined list. I
would
not be in the least surprised to find that Ruby has some magic construct
to
achieve this however. Maybe this is a challenge to the Ruby geeks to
provide the answer by the most concise (and possibly undecipherable)
code.
If you wanted all the answers for a particular company then you could
turn
it round and find all the answers where survey_question.survey.company =
the_company. But if you followed this route for all companies you would
still have the problem of grouping the answers by company which is
probably
no better.