ActiveRecord 在生产中运行不同的查询?

发布于 2024-07-16 08:04:16 字数 736 浏览 7 评论 0原文

我有一个类层次结构如下所示:

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 技术交流群。

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

发布评论

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

评论(2

风筝有风,海豚有海 2024-07-23 08:04:16

因为在生产中,所有类都会立即加载。 当所有类都加载时,它意识到 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.

可遇━不可求 2024-07-23 08:04:16

这里有一个针对此错误的开放票证:
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

引用:

您必须显式命名子类
父类

类 ProjectFeedEvent < 提要事件

def self.subclasses 
    [项目添加事件] 
  结尾   
  

结束

这个问题已经存在了一段时间并且没有受到太多关注的部分原因是 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:

You must explicitly name subclasses in
the parent class

class ProjectFeedEvent < FeedEvent

def self.subclasses
  [ProjectAddedEvent]
end  

end

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.

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