Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在ActiveRecord::关系(STI)上使用“变成” #模型 类动物'Dog')。包括(:leg)或者我写的范围应该可以工作,你使用哪个活动记录/rails版本? Zoo.first.animals(false, Dog) .includes(Leg) # This monkey patch extends the rails collection association reader (i.e. ) # zoo.animals by a second option called `class_override`. Using this option, # a custom model class for the objects to be retrieved can be specified. # # Might only work with rails 3. # # @see http://stackoverflow.com/q/25887230 module ActiveRecord module Associations class CollectionAssociation < Association #:nodoc: # Implements the reader method, e.g. foo.items for Foo.has_many :items def reader(force_reload = false, class_override = nil) if force_reload klass.uncached { reload } elsif stale_target? reload end if class_override.nil? association = self else reflection = self.reflection.dup reflection.instance_variable_set(:@klass, class_override) association = self.dup association.instance_variable_set(:@reflection, reflection) end CollectionProxy.new(association) end end end end_Ruby On Rails_Activerecord_Single Table Inheritance - Fatal编程技术网

Ruby on rails 在ActiveRecord::关系(STI)上使用“变成” #模型 类动物'Dog')。包括(:leg)或者我写的范围应该可以工作,你使用哪个活动记录/rails版本? Zoo.first.animals(false, Dog) .includes(Leg) # This monkey patch extends the rails collection association reader (i.e. ) # zoo.animals by a second option called `class_override`. Using this option, # a custom model class for the objects to be retrieved can be specified. # # Might only work with rails 3. # # @see http://stackoverflow.com/q/25887230 module ActiveRecord module Associations class CollectionAssociation < Association #:nodoc: # Implements the reader method, e.g. foo.items for Foo.has_many :items def reader(force_reload = false, class_override = nil) if force_reload klass.uncached { reload } elsif stale_target? reload end if class_override.nil? association = self else reflection = self.reflection.dup reflection.instance_variable_set(:@klass, class_override) association = self.dup association.instance_variable_set(:@reflection, reflection) end CollectionProxy.new(association) end end end end

Ruby on rails 在ActiveRecord::关系(STI)上使用“变成” #模型 类动物'Dog')。包括(:leg)或者我写的范围应该可以工作,你使用哪个活动记录/rails版本? Zoo.first.animals(false, Dog) .includes(Leg) # This monkey patch extends the rails collection association reader (i.e. ) # zoo.animals by a second option called `class_override`. Using this option, # a custom model class for the objects to be retrieved can be specified. # # Might only work with rails 3. # # @see http://stackoverflow.com/q/25887230 module ActiveRecord module Associations class CollectionAssociation < Association #:nodoc: # Implements the reader method, e.g. foo.items for Foo.has_many :items def reader(force_reload = false, class_override = nil) if force_reload klass.uncached { reload } elsif stale_target? reload end if class_override.nil? association = self else reflection = self.reflection.dup reflection.instance_variable_set(:@klass, class_override) association = self.dup association.instance_variable_set(:@reflection, reflection) end CollectionProxy.new(association) end end end end,ruby-on-rails,activerecord,single-table-inheritance,Ruby On Rails,Activerecord,Single Table Inheritance,在本例中,Rails无法知道所查询对象的特定类型(它必须分析where语句,但它似乎没有这样做)。因此,它失败了,因为关联不是在通用模型动物上定义的,而是在狗模型上定义的 有没有一种方法可以指定要检索的对象的类型,这样示例就可以运行了?我想出了一个(至少是临时的)解决方案。这个monkey补丁扩展了Rails的功能,可以在访问集合关联时指定类重写 用法: # Models class Animal < ActiveRecord::Base belongs_to :zoo end cl

在本例中,Rails无法知道所查询对象的特定类型(它必须分析
where
语句,但它似乎没有这样做)。因此,它失败了,因为关联不是在通用模型
动物
上定义的,而是在
模型上定义的

有没有一种方法可以指定要检索的对象的类型,这样示例就可以运行了?

我想出了一个(至少是临时的)解决方案。这个monkey补丁扩展了Rails的功能,可以在访问集合关联时指定类重写

用法:

# Models
class Animal < ActiveRecord::Base
  belongs_to :zoo
end

class Dog < Animal
  belongs_to :leg
end

class Snake < Animal
end

class Leg < ActiveRecord::Base
end

class Zoo
  has_many :animals
end

# Test, which fails with
# Association named 'leg' was not found on Animal; perhaps you misspelled it?
Zoo.first.animals.
         .where(:type => 'Dog')
         .includes(:leg)
猴子补丁(rails 3!):

#此monkey补丁扩展了rails集合关联阅读器(即
#通过第二个名为“class_override”的选项来设置zoo.animals。使用此选项,
#可以为要检索的对象指定自定义模型类。
#
#可能仅适用于rails 3。
#
#@见http://stackoverflow.com/q/25887230
模块活动记录
模块关联
类集合关联<关联#:nodoc:
#实现reader方法,例如foo.has\u many:items的foo.items
def读卡器(强制重新加载=false,类覆盖=nil)
如果强制重新加载
klass.uncached{reload}
elsif陈旧的目标?
重新加载
终止
如果class_覆盖.nil?
联想=自我
其他的
反射=self.reflection.dup
反射。实例变量集(:@klass,类覆盖)
关联=self.dup
association.instance_variable_set(:@reflection,reflection)
终止
CollectionProxy.new(关联)
终止
终止
终止
终止

也许,这也可以通过使用关联扩展来实现。我正在调查。

刚刚在罗伯特·潘科维茨基(Robert Pankowecki)的一篇不错的博客文章中找到了一个可能的解决方案:。

你不能定义一个具体的
有很多
动物而不是像
有腿的动物那样的
吗?嗯,我希望有更好的解决方案…
有很多:有腿的动物,>{where}(type:['Dog',…])}
然后
动物园。首先。有腿的动物。where(:type=>'Dog')。包括(:leg)
有那么可怕吗?
动物。where(:type=>'Dog')。包括(:leg)
或者我写的
范围
应该可以工作,你使用哪个
活动记录/rails
版本?
Zoo.first.animals(false, Dog)
         .includes(Leg)
# This monkey patch extends the rails collection association reader (i.e. )
# zoo.animals by a second option called `class_override`. Using this option,
# a custom model class for the objects to be retrieved can be specified.
#
# Might only work with rails 3.
#
# @see http://stackoverflow.com/q/25887230
module ActiveRecord
  module Associations
    class CollectionAssociation < Association #:nodoc:
      # Implements the reader method, e.g. foo.items for Foo.has_many :items
      def reader(force_reload = false, class_override = nil)
        if force_reload
          klass.uncached { reload }
        elsif stale_target?
          reload
        end

        if class_override.nil?
          association = self
        else
          reflection = self.reflection.dup
          reflection.instance_variable_set(:@klass, class_override)
          association = self.dup
          association.instance_variable_set(:@reflection, reflection)
        end

        CollectionProxy.new(association)
      end
    end
  end
end