【问题标题】:How can I get rid of this nasty query in my Rails view template?如何在我的 Rails 视图模板中摆脱这个讨厌的查询?
【发布时间】:2011-11-27 00:07:06
【问题描述】:

哎呀。它让我失望。

在我的控制器中:

@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

【问题讨论】:

    标签: ruby-on-rails forms model-view-controller associations


    【解决方案1】:

    哎呀,那是一个粗糙的。

    至少您可以将重复的fields_for 块拉出到一个助手中:

    module AssessorsHelper
      def answers_fields f, candidate, behavior, competency, answer=nil
        assessor = f.object
    
        f.fields_for :answers, answer do |f|
          f.hidden_field :assessor_id,    :value => assessor.id
          f.hidden_field :candidate_id,   :value => candidate.id
          f.hidden_field :behavior_id,    :value => behavior.id
          f.hidden_field :competency_id,  :value => competency.id
          f.association :answer_choice, :collection => [choice], :as => :radio
        end
      end
    end
    

    这会让你的观点变成这样:

    = 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|
              = answers_fields f, candidate, behavior, competency
    
          - else
            - competency.behaviors.each do |behavior|
              - answer = @assessor.answers.find_or_create_by_behavior_id_and_candidate_id behavior, candidate
    
              = answers_fields f, candidate, behavior, competency, answer
    

    如果你愿意,你可以将它分解为每个内部循环的帮助器,但你明白了。

    【讨论】:

    • 谢谢,乔丹。我的示例并不漂亮,但我将代码从部分中移出以简化我的问题的呈现。我喜欢你使用帮助器而不是部分的!
    猜你喜欢
    • 2021-05-18
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多