有没有办法禁用default_scope单个belongs_to关联?该default_scope是好的,但所有的单一belongs_to,我想绕过范围.我很熟悉,with_exclusive_scope但我认为不能与belongs_to一起使用.
有什么建议?
上下文:我试图允许acts_as_revisable中的branch_source关联指向不是最新的修订(是错误的).revisable_is_current
lwe*_*lwe 34
可能有点迟到(仅短短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
Run Code Online (Sandbox Code Playgroud)
更新:基于reflect_on_association https://gist.github.com/2923336获得通用解决方案
Joh*_*ith 31
belongs_to :account, -> { unscope(where: :destroyed_at) }
Run Code Online (Sandbox Code Playgroud)
适用于我,Rails 4.1
Tob*_*hen 14
我自己刚遇到这个问题,这就是我想出的:
class Comment < ActiveRecord::Base
belongs_to :document # Document has some kind of default scope
# that stops us from finding it
# Override getter method for document association
def document_with_unscoped
# Fetch document with default scope disabled
Document.unscoped { document_without_unscoped }
end
alias_method_chain :document, :unscoped
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10700 次 |
| 最近记录: |