Rails 3 中一个 AR 模型内的多个数据库表
我被要求提供某种报告(日志记录)服务。该员工在许多公司本地安装了Web应用程序(只是一些用PHP编写的动态网站)。这个网络应用程序是某种调查。所有数据都保存在本地数据库中,但现在的要求是每次提交表单后,这些数据(调查结果)也将发送到中央服务器。
有四种类型的调查。他们是这样组织的,有很多项目,每个项目每种类型只能有一个调查(这里是 STI?),而调查属于一个项目。每个调查都会收到来自本地应用程序的报告,因此它会有很多报告。记录此报告的 Rails 3 应用程序应该以某种方式模仿此逻辑。第一个问题是:这个 AR 结构对你来说有意义吗?
Project-1--------1-Survey-1-------*-Report
Project
has_one :survey
has_many :reports, :through => :survey
Survey
belongs_to :project
has_many :reports
Report
belongs_to :survey
第二个问题是关于一个 AR 模型有多个表。如果所有数据都存储在reports
表中,该表将很快变得巨大,并且在一段时间后有效查询属于特定调查的报告可能会成为问题。也许每个调查都有单独的表格会更好?就像reports_
。这可能吗?
另外,我不知何故被迫使用 MySQL,但如果有另一个更好的解决方案,我可以尝试推动它。
如果您还在这里,感谢您阅读本文:)
I was asked to make some kind of reporting (logging) service. The employee has locally installed web application (just some dynamic website, written in PHP) in many companies. This web app is some kind of survey. All data is saved on local database, but now the requirement is that this data (result of survey) will also be sent to central server after every form submit.
There are four types of surveys. They have organised it this way, that there are many Projects, and each Project can have only one Survey of each type (STI here?) and Survey belongs to one Project. Each Survey will receive a report from local app, so it will have many Reports. The Rails 3 app that logs this reports should mimic somehow this logic. First question is: does this AR structure make sense for you?
Project-1--------1-Survey-1-------*-Report
Project
has_one :survey
has_many :reports, :through => :survey
Survey
belongs_to :project
has_many :reports
Report
belongs_to :survey
Second question is about having multiple tables for one AR Model. If all data will be stored in reports
table, the table will become huge very quickly, and efficient querying for reports that belong to specific survey might be a problem after some time. Maybe it would be better to have separate tables for each Survey? Like reports_<survey_id>
. Is this possible?
Also, I am somehow forced to use MySQL, but if there is another, much better solution for this, I could try to push it through.
If you are still here, thank you for reading this :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的。
您可以这样做:
更新
Yes, it is possible.
You can do it this way:
UPDATE
在每个外键上放置一个索引(例如Reports.survey_id)并休息一下。你现在太担心表现了。在您看到 MySQL 出现任何性能问题之前,您的报告表中至少需要数百万条记录。
Put an index on each of your foreign keys (e.g. Reports.survey_id) and take a breather. You're worrying entirely too much about the performance right now. You will need at least millions of records in your Reports table before you will see any performance problems from MySQL.