有没有办法禁用单个 belongs_to
关联的 default_scope
? default_scope
对于除了我想绕过范围的单个 belongs_to
之外的所有情况都很好。我熟悉 with_exclusive_scope
但我不认为它可以与 own_to 一起使用。
有什么建议吗?
上下文:我正在尝试允许 acts_as_revisable 中的 branch_source
关联指向不是最新的修订版(revisable_is_current
为 false)。
最佳答案
可能有点晚了(只有不到 3 年),但遇到了同样的问题,Tobias 的解决方案当然是正确的方向,但可以针对 Rails 3.2+ 进行简化。我唯一仍然不喜欢的是 Document 的“硬编码”类名,也许可以使用反射来改变......
无论如何,这就是我想出的:
class Comment < ActiveRecord::Base
# Document has some kind of default_scope
belongs_to :document
# Ensure document is not scoped, because Rails 3.2 uses modules it's
# possible to use simple inheritance.
def document
Document.unscoped { super }
end
end
更新:得到了一个基于reflect_on_association https://gist.github.com/2923336的通用解决方案
关于ruby-on-rails - 如何禁用belongs_to的default_scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1540645/