Rails 方式的复杂模型
我正在一个网站上实施调查问卷。有多个调查问卷,每个调查问卷都有几个部分,每个部分都有问题。用户可以填写零个或多个调查问卷。
型号代码如下。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
result(uq_id)
方法的问题在于,它将查询数据库以获取每个结果,这在资源方面既缓慢又消耗资源。您可以做的是使用joins
或includes
方法来优化数据库访问。因此,在您的模型中:然后在控制器中:
在视图中而不是
=question.result(@user_questionnaire.id)
使用:这个想法是您从数据库加载所有部分、问题和结果,而不是每个单独的实例。以同样的方式,您可以尝试使用
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 usejoins
orincludes
methods to optimize your DB access. So in your model:Then in controller:
And in view instead of
=question.result(@user_questionnaire.id)
use: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.