1
Yuck。它讓我失望。如何在Rails視圖模板中擺脫這個討厭的查詢?
在我的控制器:
@assessor = Assessor.find(params[:id])
@assessor.answers.build if @assessor.answers.empty?
筆者認爲:
= simple_form_for @assessor do |f|
- @assessor.candidates.each do |candidate|
- @assessor.assessment_competencies.each do |competency|
- if @assessor.answers.all?{|a| a.new_record?}
- competency.behaviors.each do |behavior|
= f.fields_for :answers do |f|
- @assessor.standard_answer_choices.each do |choice|
= f.input :assessor_id, :as => :hidden, :input_html => {:value => @assessor.id}
= f.input :candidate_id, :as => :hidden, :input_html => {:value => candidate.id}
= f.input :behavior_id, :as => :hidden, :input_html => {:value => behavior.id}
= f.input :competency_id, :as => :hidden, :input_html => {:value => competency.id}
= f.association :answer_choice, :collection => [choice], :as => :radio
- else
- competency.behaviors.each do |behavior|
- answer = Answer.find_or_create_by_behavior_id_and_assessor_id_and_candidate_id(behavior.id, @assessor.id, candidate.id)
= f.fields_for :answers, answer do |f|
= f.input :assessor_id, :as => :hidden, :input_html => {:value => @assessor.id}
= f.input :candidate_id, :as => :hidden, :input_html => {:value => candidate.id}
= f.input :behavior_id, :as => :hidden, :input_html => {:value => behavior.id}
= f.input :competency_id, :as => :hidden, :input_html => {:value => competency.id}
= f.association :answer_choice, :collection => [choice], :as => :radio
謝謝,喬丹。我的例子並不漂亮,但我爲了簡化問題的表達而將代碼移出部分。我喜歡你的幫手而不是部分的使用! – Blastula