返回类型从ActiveRecord的查询 [英] Return type from a ActiveRecord query
问题描述
有什么可以让我知道,如果我要得到一个关系,阵列,或一些其他类型从ActiveRecord的回调?我知道我可以输入的.class在控制台和数字出来,但有什么东西在召唤自己,让我知道我要求的?
What will let me know if I am going to get a Relation, Array, or some other type back from an ActiveRecord call? I know I can type .class in the console and figure it out, but is there something in the call itself that will let me know what I am asking for?
推荐答案
您知道,有时Rails的谎言给你 - 所有的魔术师做的:)
You know, Rails sometimes lies to you -- all magicians do :)
Rails允许你建立复杂的查询,通过链接你的的has_many
的关联。此功能的核心是一堆XXXAssocation(比如 HasManyAssociation
)类。
当你调用的.class
在的has_many
的关联你的电话,其实是在申请 HasManyAssociation
实例。但这里的神奇启动:
Rails allows you to build complex queries by chaining your has_many
associations. The core of this functionality is a bunch of XXXAssocation (like HasManyAssociation
) classes.
When you call .class
on a has_many
association your call is applied in fact for HasManyAssociation
instance. But here's the magic starts:
# collection_proxy.rb
instance_methods.each { |m| undef_method m unless m.to_s =~ /^(?:nil\?|send|object_id|to_a)$|^__|^respond_to|proxy_/ }
Rails的undefs(隐藏) HasManyAssociation
实例的方法(除了少数几个,你可以在常规EX pression看到的),然后使用委派和的method_missing
您的来电传递给一些潜在的阵列(如果你想获取记录)或协会本身(如果你链接您的关联):
Rails undefs (hides) methods of HasManyAssociation
instance (except the few, as you can see in the regular expression) and then uses delegation and method_missing
to pass your call to some underlying array (if you're trying to fetch records) or to association itself (if you're chaining your association):
delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from,
:lock, :readonly, :having, :pluck, :to => :scoped
delegate :target, :load_target, :loaded?, :to => :@association
delegate :select, :find, :first, :last,
:build, :create, :create!,
:concat, :replace, :delete_all, :destroy_all, :delete, :destroy, :uniq,
:sum, :count, :size, :length, :empty?,
:any?, :many?, :include?,
:to => :@association
def method_missing(method, *args, &block)
match = DynamicFinderMatch.match(method)
if match && match.instantiator?
send(:find_or_instantiator_by_attributes, match, match.attribute_names, *args) do |r|
proxy_association.send :set_owner_attributes, r
proxy_association.send :add_to_target, r
yield(r) if block_given?
end
end
if target.respond_to?(method) || (!proxy_association.klass.respond_to?(method) && Class.respond_to?(method))
if load_target
if target.respond_to?(method)
target.send(method, *args, &block)
else
begin
super
rescue NoMethodError => e
raise e, e.message.sub(/ for #<.*$/, " via proxy for #{target}")
end
end
end
else
scoped.readonly(nil).send(method, *args, &block)
end
end
因此, HasManyAssociation
实例决定什么需要自己来处理,哪些需要通过隐藏的阵列来实现(类
方法是不是 HasManyAssociation
有兴趣,因此会被调用这个隐藏阵列上,结果,当然,将是阵列
,这是一个小把戏)。
So, HasManyAssociation
instance decides what to handle by itself and what needs to be accomplished via hidden array (class
method isn't what HasManyAssociation
interested in so it will be called on this hidden array. The result, of course, will be Array
, which is a little deception).
这篇关于返回类型从ActiveRecord的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!