ruby-on-rails - 如何禁用belongs_to的default_scope?

标签 ruby-on-rails

有没有办法禁用单个 belongs_to 关联的 default_scopedefault_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/

相关文章:

ruby-on-rails - 如何在不使用 eval 方法的情况下使用带有参数化代码的 RSpec 创建共享示例以进行测试?

ruby-on-rails - 是否可以在不同的数据类型中使用 more_like_this ?

ruby-on-rails - 创建动态集合选择rails 3.1

javascript - 使用 Rails 和 Haml 的 Jquery 和 CoffeeScript scrollTo

ruby-on-rails - 如何检查 Rails 更新操作中属性是否发生变化?

ruby-on-rails - "DateTime.beginning_of_week => :monday"但 "DateTime.current.wday == 0"是星期日

html - @font-family,奇怪的字体名称 - 我可以重命名吗?

javascript - Twitter Bootstrap 粘性子导航在它应该在较短的页面上之前变得固定

ruby-on-rails - 如何同时使用不同版本的 gem

ruby-on-rails - 在 Rails 中将时间从一个时区转换为另一个时区