带连接的嵌套命名范围(爆炸性错误)

发布于 2024-08-18 17:45:16 字数 416 浏览 4 评论 0原文

所以我有一个 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 技术交流群。

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

发布评论

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

评论(1

天涯离梦残月幽梦 2024-08-25 17:45:16

您正在寻找

Model.with_exclusive_scope { ...do your find in here... } 

这将删除该块当前使用的任何范围。

用法示例:

# in model.rb
def self.find_stuff
  self.scope_with_some_joins.find_in_batches do |models|
    models.each do |mdl|
      self.with_exclusive_scope do
        other_comparisons = self.scope_with_other_joins
      end
    end
  end
end

然后使用 Model.find_stuff 进行查询。这样,逻辑就包含在模型中,而不是控制器中。

You're looking for

Model.with_exclusive_scope { ...do your find in here... } 

This will remove any scopes that are currently in use for the block.

An example usage:

# in model.rb
def self.find_stuff
  self.scope_with_some_joins.find_in_batches do |models|
    models.each do |mdl|
      self.with_exclusive_scope do
        other_comparisons = self.scope_with_other_joins
      end
    end
  end
end

Then you query with Model.find_stuff. This way the logic is wrapped up in the model, not in the controller.

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