ActiveRecord中的Ruby类型的集合

kmo*_*511 10 ruby activerecord

如果我在ActiveRecord中有一个带有子对象集合的对象,即

class Foo < ActiveRecord::Base
  has_many :bars, ...
end
Run Code Online (Sandbox Code Playgroud)

我尝试find对该集合运行Array的方法:

foo_instance.bars.find { ... }
Run Code Online (Sandbox Code Playgroud)

我收到:

ActiveRecord::RecordNotFound: Couldn't find Bar without an ID
Run Code Online (Sandbox Code Playgroud)

我认为这是因为ActiveRecord find为了自己的目的而劫持了该方法.现在,我可以使用detect,一切都很好.然而,为了满足我自己的好奇心,我尝试使用元编程来明确地将该find方法偷回一次:

unbound_method = [].method('find').unbind
unbound_method.bind(foo_instance.bars).call { ... }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

TypeError: bind argument must be an instance of Array
Run Code Online (Sandbox Code Playgroud)

很明显Ruby并不认为foo_instance.bars是一个数组而且:

foo_instance.bars.instance_of?(Array) -> true
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解释一下这个以及通过元编程来解决它的方法吗?

Sim*_*tti 9

我认为这是因为ActiveRecord为了自己的目的而劫持了find方法.

这不是真正的解释.foo_instance.bars不返回Array的实例而是返回的实例ActiveRecord::Associations::AssociationProxy.这是一个特殊类,旨在充当持有关联的对象和关联对象之间的代理.

AssociatioProxy对象充当数组,但它实际上不是数组.以下详细信息直接来自文档.

# Association proxies in Active Record are middlemen between the object that
# holds the association, known as the <tt>@owner</tt>, and the actual associated
# object, known as the <tt>@target</tt>. The kind of association any proxy is
# about is available in <tt>@reflection</tt>. That's an instance of the class
# ActiveRecord::Reflection::AssociationReflection.
#
# For example, given
#
#   class Blog < ActiveRecord::Base
#     has_many :posts
#   end
#
#   blog = Blog.find(:first)
#
# the association proxy in <tt>blog.posts</tt> has the object in +blog+ as
# <tt>@owner</tt>, the collection of its posts as <tt>@target</tt>, and
# the <tt>@reflection</tt> object represents a <tt>:has_many</tt> macro.
#
# This class has most of the basic instance methods removed, and delegates
# unknown methods to <tt>@target</tt> via <tt>method_missing</tt>. As a
# corner case, it even removes the +class+ method and that's why you get
#
#   blog.posts.class # => Array
#
# though the object behind <tt>blog.posts</tt> is not an Array, but an
# ActiveRecord::Associations::HasManyAssociation.
#
# The <tt>@target</tt> object is not \loaded until needed. For example,
#
#   blog.posts.count
#
# is computed directly through SQL and does not trigger by itself the
# instantiation of the actual post records.
Run Code Online (Sandbox Code Playgroud)

如果您想处理结果数组,则根本不需要元编程技能.只需进行查询并确保在真实的Array对象上调用find方法,而不是在类似数组的quack上调用.

foo_instance.bars.all.find { ... }
Run Code Online (Sandbox Code Playgroud)

all方法是ActiveRecord finder方法(find(:all)的快捷方式).它返回一个array结果.然后,您可以Array#find在数组实例上调用该方法.


ger*_*rit 6

正如其他人所说,关联对象实际上并不是一个数组.要找出真正的类,请在irb中执行此操作:

class << foo_instance.bars
  self
end
# => #<Class:#<ActiveRecord::Associations::HasManyAssociation:0x1704684>>

ActiveRecord::Associations::HasManyAssociation.ancestors
# => [ActiveRecord::Associations::HasManyAssociation, ActiveRecord::Associations::AssociationCollection, ActiveRecord::Associations::AssociationProxy, Object, Kernel]
Run Code Online (Sandbox Code Playgroud)

要删除在执行foo_instance.bars.find()时调用的ActiveRecord :: Bse #ref方法,以下内容将有所帮助:

class << foo_instance.bars
  undef find
end
foo_instance.bars.find {...} # Array#find is now called
Run Code Online (Sandbox Code Playgroud)

这是因为AssociationProxy类将它不知道的所有方法(通过method_missing)委托给它的#target,它是实际的底层数组实例.