带连接的嵌套命名范围(爆炸性错误)
所以我有一个 ActiveRecord 类,它有几个不同的命名范围,其中包括连接参数。在运行报告时,我碰巧遇到了一种情况,其中一个在另一个内部被调用:
1 Model.scope_with_some_joins.find_in_batches do |models|
2 models.each do |mdl|
3 other_comparisons = Model.scope_with_other_joins
4 end
5 end
我的问题在第 3 行 - 我收到一个运行时错误,显示由于某种原因,在运行第二个查询时,它正在维护来自的连接范围外部查询。实际上,我需要它单独运行,而不与外部查询共享上下文。有什么想法或想法吗?
(我应该提到这个问题是一个“不明确的列”错误,因为有一个表是从两个查询中加入的)
So I have an ActiveRecord class with a couple different named scopes that include join parameters. While running a report, I happen to have a situation where one gets called inside of the other:
1 Model.scope_with_some_joins.find_in_batches do |models|
2 models.each do |mdl|
3 other_comparisons = Model.scope_with_other_joins
4 end
5 end
My problem is on line 3 -- I get a runtime error showing me that for some reason when running the second query it's maintaining the join scope from the outer query. Really I need it to be run separately on it's own, without sharing context with the outer query. Any thoughts or ideas?
(I should mention that the problem is an "ambigious column" error because there is one table that is joined in from both queries)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找
这将删除该块当前使用的任何范围。
用法示例:
然后使用
Model.find_stuff
进行查询。这样,逻辑就包含在模型中,而不是控制器中。You're looking for
This will remove any scopes that are currently in use for the block.
An example usage:
Then you query with
Model.find_stuff
. This way the logic is wrapped up in the model, not in the controller.