【发布时间】:2013-05-09 16:28:41
【问题描述】:
我正在从 Rails 2.3.5 升级到 2.3.17,但遇到了一个非常模糊的问题。我使用以下扩展在关联访问器中注入范围,并提供自定义构建器:
module XYZFilterExtension
def in_context(context)
if proxy_owner.context == :abc && context != :admin
scoped(:conditions => {:startup => false})
else
scoped({})
end
end
end
module OtherExtension
def build_with_component_instance(attributes = nil)
attributes ||= {}
attributes.reverse_merge!(:parent_id => proxy_owner.component_instance.id)
instance = build
instance.build_component_instance
instance.attributes = attributes
instance
end
end
has_many :pages, :extend => [ABCExtension, OtherExtension]
在 Rails 2.3.5 中我可以调用:
Something.pages.in_context(:abc).build_with_component_instance
我会得到一个 Page 对象(它与 component_instance 相关联,构建很复杂,因为它是从另一个方向构建的多态关联)。
现在我明白了:
undefined method `build_with_component_instance' for #<Class:0x1151dcae8>
检查范围,我能发现的唯一区别是在in_context()创建的范围上调用proxy_scope会返回2.3.17中的Page模型和2.3.5中的范围。
我不知道从这里去哪里。我无法将范围提取到一个模块中以包含在每个模型中,因为我需要根据关联中的proxy_owner 做出决定。
更新:问题似乎在于扩展方法在范围的上下文中不可用。很奇怪,但我想这有点道理。不幸的是,我的范围定义和构建扩展都需要了解它们的关联上下文。欢迎任何想法:)
【问题讨论】: