Rails 方式的复杂模型

发布于 2024-11-02 11:56:01 字数 1343 浏览 0 评论 0原文

我正在一个网站上实施调查问卷。有多个调查问卷,每个调查问卷都有几个部分,每个部分都有问题。用户可以填写零个或多个调查问卷。

型号代码如下。 ERD 图像 https://i.sstatic.net/6Y0r3.png

什么是“Rails”方式”用于存储/显示问题结果,同时维护视图中的部分层次结构?我不介意编写代码,但不确定我是否遗漏了一些明显的东西。我已经从问题模型中的方法开始,但它没有利用表单助手等。

#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

I'm implementing questionnaires on a site. There are multiple questionnaires, each have sections, each section has questions. Users can fill out zero or many questionnaires.

model code below. ERD image https://i.sstatic.net/6Y0r3.png

What would be the "Rails Way" for storing/displaying question results while maintaining section hierarchy in the view? I don't mind writing code, but wasn't sure if I was missing something obvious. I've started with methods in the Question model but it doesn't take advantage of Form Helpers and the like.

#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) 

Any thoughts are welcome. Thanks!
Donald

Model Code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听闻余生 2024-11-09 11:56:01

您的 result(uq_id) 方法的问题在于,它将查询数据库以获取每个结果,这在资源方面既缓慢又消耗资源。您可以做的是使用 joinsincludes 方法来优化数据库访问。因此,在您的模型中:

class UserQuestionnaire
    def self.includes_questions
      includes :questionnaire => {:sections => [:questions]}
    end

    def loaded_result question
      user_questionnaire_results.to_a.find { |r| r.question_id == question.id }
    end
end

然后在控制器中:

@user_questionnaire = UserQuestionnaire.includes_questions.find params[:id]

在视图中而不是 =question.result(@user_questionnaire.id) 使用:

= @user_questionnaire.loaded_result question

这个想法是您从数据库加载所有部分、问题和结果,而不是每个单独的实例。以同样的方式,您可以尝试使用 joins 函数,看看它是否更适合您。

检查开发环境中的日志或控制台以查看执行了哪些查询。

What's wrong with your result(uq_id) method is that it will query DB for every result, which is slow and consty resource-wise. What you could do is to use joins or includes methods to optimize your DB access. So in your model:

class UserQuestionnaire
    def self.includes_questions
      includes :questionnaire => {:sections => [:questions]}
    end

    def loaded_result question
      user_questionnaire_results.to_a.find { |r| r.question_id == question.id }
    end
end

Then in controller:

@user_questionnaire = UserQuestionnaire.includes_questions.find params[:id]

And in view instead of =question.result(@user_questionnaire.id) use:

= @user_questionnaire.loaded_result question

The idea is that you load from DB all sections, questions and result togather, not each separate instance. In the same manner you may try to play with joins function and see if it's better for you.

Check logs or console in dev env to see which queries are executed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文