查询多态关联
我有一个像这样的多态关联 -
class Image < ActiveRecord::Base
has_one :approval, :as => :approvable
end
class Page < ActiveRecord::Base
has_one :approval, :as => :approvable
end
class Site < ActiveRecord::Base
has_one :approval, :as => :approvable
end
class Approval < ActiveRecord::Base
belongs_to :approvable, :polymorphic => true
end
我需要找到批准,其中 approval.apporvable.deleted = false
我尝试过类似的操作 -
@approvals = Approval.find(:all,
:include => [:approvable],
:conditions => [":approvable.deleted = ?", false ])
这给出了“无法急切加载多态关联:approvable”错误
如何是否可以正确给出条件,以便我获得批准的结果集,谁的可批准项目未被删除?
感谢您提前提供的任何帮助
I have a polymorphic association like this -
class Image < ActiveRecord::Base
has_one :approval, :as => :approvable
end
class Page < ActiveRecord::Base
has_one :approval, :as => :approvable
end
class Site < ActiveRecord::Base
has_one :approval, :as => :approvable
end
class Approval < ActiveRecord::Base
belongs_to :approvable, :polymorphic => true
end
I need to find approvals where approval.apporvable.deleted = false
I have tried something like this -
@approvals = Approval.find(:all,
:include => [:approvable],
:conditions => [":approvable.deleted = ?", false ])
This gives "Can not eagerly load the polymorphic association :approvable" error
How can the condition be given correctly so that I get a result set with approvals who's approvable item is not deleted ?
Thanks for any help in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是不可能的,因为所有“可批准的”都位于不同的表中。相反,您必须获取所有批准,然后使用普通的数组方法。
This is not possible, since all "approvables" reside in different tables. Instead you will have to fetch all approvals, and then use the normal array methods.
就 SQL 而言,您的要求是将来自不同表的数据投影到结果集中的不同行。据我所知这是不可能的。
所以你必须满足于:
What your asking, in terms of SQL, is projecting data from different tables for different rows in the resultset. It is not possible to my knowledge.
So you'll have to be content with:
我会推荐这里已经提供的任何一个答案(它们是同一件事),但如果您真的想在单个查询中完成所有操作,我还建议将已删除的标志放入审批模型中。
使用多态关系,rails 可以在多边形上使用急切获取,但您无法加入它们,因为同样,关系是未知的,因此查询实际上是多个交叉查询。
因此,最后,如果您确实需要,请放入 sql 中并交叉所有可能的连接,您可以在单个查询中对所有类型的可批准事项执行此操作,但您将必须手动执行大量连接。 (手动意味着不使用 Rails 的内置机制......)
I would recommend either of the answers already presented here (they are the same thing) but I would also recommend putting that deleted flag into the Approval model if you really care to do it all in a single query.
With a polymorphic relationship rails can use eager fetching on the polys, but you can't join to them because yet again, the relationships are not known so the query is actually multiple queried intersected.
So in the end if you REALLY need to, drop into sql and intersect all the possible joins you can do to all the types of approvables in a single query, but you will have to do lots of joining manually. (manually meaning not using rails' built-in mechanisms...)
谢谢你的回答
我非常确定这是不可能的。我想要更多确认
除此之外,我希望有一些其他的解决方案,而不是循环遍历结果集
以避免以后出现与性能相关的问题
虽然暂时拒绝/选择都很好,但从长远来看我
必须手动执行这些 sql 连接。
再次感谢您的帮助!!
中号
thanks for your answers
I was pretty sure that this couldn't be done. I wanted some more confirmation
besides that I was hoping for some other soln than looping thru the result set
to avoid performance related issues later
Although for the time being both reject/select are fine but in the long run I
will have to do those sql joins manually.
Thanks again for your help!!
M