我有一个模块,我将其包含在几个具有此内容的模型中:
self.class.find_by_foo(bar)
一切都很好,直到我开始使用 STI。该行应始终生成查询
select * from table where foo=bar"
而不是
select * from table where foo=bar AND type="Whatever"
有没有一种简单的方法来避免它?
我虽然有两种解决方案。遍历类层次结构,直到我找到之前的最顶层类ActiveRecord::Base
或手动运行查询,例如:
self.class.find_by_sql("select * from #{self.class.table_name} where foo=bar")
我不喜欢这两种解决方案。有更好的吗?