0
我從Rails 2.3.5升級到2.3.17,遇到了一個相當模糊的問題。我用下面的擴展注入關聯訪問內的範圍,並提供一個自定義生成器:在Rails中通過關聯擴展插入範圍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
,我會得到一個頁面對象(它與component_instance
相關聯,構建複雜,因爲它是從另一個方向構建的多態關聯)。
現在,我得到:
undefined method `build_with_component_instance' for #<Class:0x1151dcae8>
檢查的範圍,我能找到的唯一的區別是,呼籲通過in_context()
創建範圍proxy_scope
返回2.3.17頁模式,在2.3的範圍。 5。
我不確定該從哪裏出發。我無法將範圍提取到模塊中以包含在每個模型中,因爲我需要根據關聯中的proxy_owner
做出決定。
更新:看來問題是圍繞範圍內的擴展方法不可用。很奇怪,但我想這是有道理的。不幸的是,我的範圍定義和構建擴展都需要知道其關聯上下文。任何想法歡迎:)