2
我採取在網站問卷調查。有多個問卷,每個都有章節,每個章節都有問題。用戶可以填寫零個或多個問卷。複合模型Rails的方式
模型下面的代碼。 ERD圖像http://i.stack.imgur.com/6Y0r3.png
會是什麼「的Rails之路」,用於存儲/顯示問題的結果,同時維持部層次的看法?我不介意寫代碼,但不知道我是否錯過了一些明顯的東西。我已經開始使用Question模型中的方法,但它沒有利用Form Helper等。
#Simplistic view code
%h1= @user_questionnaire.questionnaire.value
- for section in @user_questionnaire.questionnaire.sections
%h4=section.value
%br
- for question in section.questions
=question.value
=question.result(@user_questionnaire.id)
歡迎任何想法。謝謝! 唐納德
型號代碼
class Questionnaire < ActiveRecord::Base
has_many :sections
has_many :user_questionnaires
end
class Section < ActiveRecord::Base
belongs_to :questionnaire
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :section
has_many :user_questionnaire_results
def result(uq_id)
uqr = UserQuestionnaireResult.where(:question_id => self.id, :user_questionnaire_id => uq_id).first
uqr.result
end
end
class UserQuestionnaire < ActiveRecord::Base
belongs_to :questionnaire
has_many :user_questionnaire_results
belongs_to :user
end
class UserQuestionnaireResult < ActiveRecord::Base
belongs_to :user_questionnaire
belongs_to :question
end
對不起,讓周圍以測試這一點的延遲,但它原來是一個非常乾淨,乾燥的方法。正是我在找什麼。謝謝! – DonaldSowell 2011-05-12 18:43:10