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?
有谁知道为什么会这样,有没有办法让它至少表现得一致,如果没有彻底解决问题?
2 个解决方案
#1
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
这里有一个这个错误的门票: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
该解决方案列在此票证的底部: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
class ProjectFeedEvent
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
这个问题已经存在一段时间并且没有引起太多关注的部分原因是STI在Rails中并不常见。 Rails的大多数贡献者决定不在自己的项目中使用它,因此没有花时间确保它得到很好的支持。这里有一个简单的解释,为什么你不应该使用它并提出一个替代方案: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.
我在公司使用STI的个人经历起初看起来非常有用,但随着时间的推移,我们确定我们根本不需要它来保证复杂性。从那时起,我们的项目发展迅速,我们根本没有错过它。
#2
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.
因为在生产中,所有类都会立即加载。当所有类都加载时,它意识到ProjectDesignWall是Project的子类,因此收集所有类。
#1
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
这里有一个这个错误的门票: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
该解决方案列在此票证的底部: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
class ProjectFeedEvent
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
这个问题已经存在一段时间并且没有引起太多关注的部分原因是STI在Rails中并不常见。 Rails的大多数贡献者决定不在自己的项目中使用它,因此没有花时间确保它得到很好的支持。这里有一个简单的解释,为什么你不应该使用它并提出一个替代方案: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.
我在公司使用STI的个人经历起初看起来非常有用,但随着时间的推移,我们确定我们根本不需要它来保证复杂性。从那时起,我们的项目发展迅速,我们根本没有错过它。
#2
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.
因为在生产中,所有类都会立即加载。当所有类都加载时,它意识到ProjectDesignWall是Project的子类,因此收集所有类。