- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题
似乎 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>
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
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
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
def current_scope #:nodoc:
ScopeRegistry.value_for(:current_scope, base_class.to_s)
end
def unscoped #:nodoc:
block_given? ? relation.scoping { yield } : relation
end
def scoping
@klass.with_scope(self, :overwrite) { yield }
end
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.preload(:unscoped_checklist).where(id: 1)
关于ruby-on-rails-4 - 为什么这些嵌套的无作用域 block 在 Rails 3 中工作时不能删除 Rails 4 中的 default_scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22341279/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!