gpt4 book ai didi

ruby-on-rails-4 - 为什么这些嵌套的无作用域 block 在 Rails 3 中工作时不能删除 Rails 4 中的 default_scope?

转载 作者:行者123 更新时间:2023-12-02 03:37:43 25 4
gpt4 key购买 nike

我的问题

似乎 Rails 4 忽略了嵌套的无作用域块(而它们在 Rails 3 中没问题)。我一直在疯狂地谷歌搜索,在这里找不到任何表明变化的东西。有什么想法可以让我在 Rails 4 中工作吗?

我在做什么

我将 default_scope 用于 Multi-Tenancy ,如 #388 Multitenancy with Scopes 所示.一些管理员将是多个租户的管理员,我想在报告中向他们显示汇总数据。我是通过使用 unscoped blocks 来做到这一点的.我还预加载了关联的对象,因为 1) 它更高效,2) 我需要将所有关联的对象放在一个地方,这样我就不必在我的 View 中继续使用无作用域的块。为了预加载关联的对象,我使用了嵌套的无作用域块。

我在 Rails 3.2.17 上的应用程序中使用了这个,但现在我已经升级到 Rails 4.0.1.rc1,它不再起作用。

一个简单的例子来说明差异

下面,我将展示我在控制台中得到的东西。这个例子比我真正想做的要简单得多,但我认为这是展示正在发生的事情的最简单方法。

Loading development environment (Rails 3.2.17)
1.9.3-p374 :001 > s = nil
=> nil
1.9.3-p374 :002 > Submission.unscoped { Checklist.unscoped { s = Submission.preload(:checklist).find(3269) }}
Submission Load (27.8ms) SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 LIMIT 1 [["id", 3269]]
Checklist Load (0.6ms) SELECT "checklists".* FROM "checklists" WHERE "checklists"."id" IN (17)
=> #<Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank">
1.9.3-p374 :003 > s
=> #<Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank">
1.9.3-p374 :004 > s.checklist
=> #<Checklist id: 17, name: "Open", description: "Chores Required To Open Store Front", creator_id: 2, created_at: "2013-09-23 21:55:23", updated_at: "2013-09-23 21:55:23", archived: false, archived_at: nil, ancestry: nil, tenant_id: 2>

所以,我正在加载提交并预加载其相关的 list 。然后我可以确认提交及其相关的 list 都可用。

如果我切换到我的 Rails 4 环境(都在同一个数据库上工作),我看到的是:
 Loading development environment (Rails 4.1.0.rc1)
1.9.3-p374 :001 > s = nil
=> nil
1.9.3-p374 :002 > Submission.unscoped { Checklist.unscoped { s = Submission.preload(:checklist).find(3269) }}
Submission Load (0.4ms) SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 LIMIT 1 [["id", 3269]]
Checklist Load (0.6ms) SELECT "checklists".* FROM "checklists" WHERE "checklists"."tenant_id" IS NULL AND "checklists"."id" IN (17)
=> #<Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank">
1.9.3-p374 :003 > s
=> #<Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank">
1.9.3-p374 :004 > s.checklist
=> nil

我正在运行完全相同的代码,但只有提交可用 - 其关联的 list 为零。

FWIW,如果我在控制台中运行它,我可以在 Rails 3.2.17 中复制它。 注意:这里的区别是我只使用 Submission.unscoped {} 块,而不是嵌套 Checklist.unscoped {} 块。
  Loading development environment (Rails 3.2.17)
1.9.3-p374 :001 > s = nil
=> nil
1.9.3-p374 :002 > Submission.unscoped { s = Submission.preload(:checklist).find(3269) }
Submission Load (3.6ms) SELECT "submissions".* FROM "submissions" WHERE "submissions"."id" = $1 LIMIT 1 [["id", 3269]]
Checklist Load (0.4ms) SELECT "checklists".* FROM "checklists" WHERE "checklists"."tenant_id" IS NULL AND "checklists"."id" IN (17)
=> #<Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank">
1.9.3-p374 :003 > s
=> #<Submission id: 3269, user_id: nil, workday_id: 17, checklist_id: 17, note: nil, submitted: false, submitted_at: nil, created_at: "2014-03-12 01:06:03", updated_at: "2014-03-12 01:06:03", for_date: "2014-03-11", tenant_id: 2, position: 1, department_id: nil, status: "blank">
1.9.3-p374 :004 > s.checklist
=> nil

更新

我深入研究了 Rails 源代码,可以看到 3.2-stable 中的“unscoped”方法与当前主方法相同,但该方法调用“relation.scoping”,并且该方法在两个分支之间有很大不同。我不确定是否引入了错误,或者预期的行为是否发生了变化。

以供引用:

current master
def unscoped
block_given? ? relation.scoping { yield } : relation
end

关系范围
def scoping
previous, klass.current_scope = klass.current_scope, self
yield
ensure
klass.current_scope = previous
end

current_scope
def current_scope #:nodoc:
ScopeRegistry.value_for(:current_scope, base_class.to_s)
end

3.2-stable
def unscoped #:nodoc:
block_given? ? relation.scoping { yield } : relation
end

关系范围
def scoping
@klass.with_scope(self, :overwrite) { yield }
end

链接到 with_scope方法...
def with_scope
# Pretty long and involved method
# See Line 60 in the linked doc
end

最佳答案

看起来我偶然发现了一个 Rails 4 错误。据我所知,目前在 Rails 4 中无法同时使用无作用域加载和预先加载,因为似乎“无作用域”本质上不会嵌套或链接。

这是 Github 上的问题:

Using Includes and Unscoped #11036

我最终做的(现在)是在我需要的地方创建无作用域的关联(现在在 Rails 4 中可用)。这是一个示例(来自我添加到 Github 问题的要点):

class Submission < ActiveRecord::Base
default_scope { where(tenant_id: Tenant.current_id) }

belongs_to :checklist
###### This unscoped association is my workaround in Rails 4
###### You'll want comment out if testing in Rails 3
belongs_to :unscoped_checklist, -> { unscope(where: :tenant_id) }, foreign_key: :checklist_id, class_name: "Checklist"
belongs_to :tenant
end

所以,我可以调用submission.unscoped_checklist 来绕过default_scope。但是请注意,这不能与预先加载一起使用。例如,这将不起作用:
Submission.unscoped.preload(:unscoped_checklist).where(id: 1)

将返回提交,但随后调用 submit.unscoped_checklist 将返回 nil。

因此,我目前无法急切加载无作用域关联,但我已经想出了如何在需要时访问无作用域关联。

关于ruby-on-rails-4 - 为什么这些嵌套的无作用域 block 在 Rails 3 中工作时不能删除 Rails 4 中的 default_scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22341279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com