ActiveRecord 在生产中运行不同的查询?
我有一个类层次结构如下所示:
class Post < ActiveRecord::Base; end
class Project < Post; end
class ProjectDesignWall < Project; end
有一个控制器可以像这样获取数据:
@projects = Project.find(:all, :include => [:project_image_photos,:user])
在 development
中,它直接从日志运行以下查询:
SELECT * FROM `posts` WHERE ( (`posts`.`type` = 'Project' ) ) ORDER BY originally_created_at DESC
但是,一旦它在 中运行生产模式,即使使用相同的数据库和数据,也会产生以下查询:
SELECT * FROM `posts` WHERE ( (`posts`.`type` = 'Project' OR `posts`.`type` = 'ProjectDesignWall' ) ) ORDER BY originally_created_at DESC
有谁知道为什么会发生这种情况,有没有办法让它至少表现一致,如果不能彻底解决问题?
I have a class hierarchy looks like this:
class Post < ActiveRecord::Base; end
class Project < Post; end
class ProjectDesignWall < Project; end
There's a controller that fetches data like so:
@projects = Project.find(:all, :include => [:project_image_photos,:user])
In development
, this runs the following query, straight from the logs:
SELECT * FROM `posts` WHERE ( (`posts`.`type` = 'Project' ) ) ORDER BY originally_created_at DESC
However, as soon as it's run in production
mode, even with the same database and data, it results in the following query:
SELECT * FROM `posts` WHERE ( (`posts`.`type` = 'Project' OR `posts`.`type` = 'ProjectDesignWall' ) ) ORDER BY originally_created_at DESC
Does anyone know why this is happening, and is there any way to get it to at least behave consistantly, if not outright fix the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为在生产中,所有类都会立即加载。 当所有类都加载时,它意识到 ProjectDesignWall 是 Project 的子类,因此会收集所有类。
Because in production all your classes are loaded at once. When all the classes are loading it realises that ProjectDesignWall is a subclass of Project thus gathers all of them.
这里有一个针对此错误的开放票证:
https:// rails.lighthouseapp.com/projects/8994/tickets/188-single-table-inheritance-bug-only-in-product-environment
解决方案列于本票底部:https://rails.lighthouseapp。 com/projects/8994-ruby-on-rails/tickets/2389-sti-changes-behavior-depending-on-environment
引用:
这个问题已经存在了一段时间并且没有受到太多关注的部分原因是 STI 在 Rails 中通常不是必需的。 大多数 Rails 贡献者都决定不在自己的项目中使用它,因此不会花时间确保它得到良好的支持。 以下简介简要解释了为什么不应使用它并建议了替代方案: http://www.matthewpaulmoore.com/ruby-on-rails-code-quality-checklist#sti
我自己在公司使用 STI 的个人经验是,一开始似乎非常有用,但随着随着时间的推移,我们发现我们根本不需要它来保证复杂性。 从那时起,我们的项目急剧增长,我们一点也没有错过。
There is an open ticket for this bug here:
https://rails.lighthouseapp.com/projects/8994/tickets/188-single-table-inheritance-bug-only-in-production-environment
The solution is listed at the bottom of this ticket: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2389-sti-changes-behavior-depending-on-environment
To quote:
Part of the reason this issue has been around for a while and has not received much attention is that STI is not commonly necessary in Rails. Most contributors to Rails have decided not to use it in their own projects and therefore do not put time into making sure that it is well supported. Here's a blurb that briefly explains why you should not use it and suggests an alternative: http://www.matthewpaulmoore.com/ruby-on-rails-code-quality-checklist#sti
My own personal experiences using STI at my company was that is seemed very useful at first, but as time went on we determined that we simply didn't need it enough to warrant the complexity. Since then, our project has grown dramatically and we have not missed it at all.