【问题标题】:Injecting a scope through an association extension in Rails 2.3.17通过 Rails 2.3.17 中的关联扩展注入范围
【发布时间】: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 做出决定。

更新:问题似乎在于扩展方法在范围的上下文中不可用。很奇怪,但我想这有点道理。不幸的是,我的范围定义和构建扩展都需要了解它们的关联上下文。欢迎任何想法:)

【问题讨论】:

    标签: ruby-on-rails-2 scopes


    【解决方案1】:

    我最终没有找到解决此问题的方法。最后我不得不在它发生的特定情况下避免这个错误。幸运的是,它只在应用中的几个地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2013-06-09
      • 1970-01-01
      相关资源
      最近更新 更多