在ActiveRecord的多态关联的预先加载 [英] Eager loading of polymorphic associations in ActiveRecord

查看:185
本文介绍了在ActiveRecord的多态关联的预先加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用Rails我的第一次,我想知道是否有可能加载了一个SQL查询一个多态的关联?这些模型和它们之间的关系是基本够用了:一个资产模型/表可以参考的内容(无论是图片,文字或声音)通过一个多态关联,即

This is my first time using Rails and I was wondering if it's possible to load a has one polymorphic association in one SQL query? The models and associations between them are basic enough: An Asset model/table can refer to a content (either an Image, Text, or Audio) through a polymorphic association, i.e.


class Asset < ActiveRecord::Base
  :belongs_to :content, :polymorphic => true
end

和图像,文字,声音的定义是这样的:

and the Image, Text, Audio are defined like this:


class Image < ActiveRecord::Base
  :has_one :asset, :as => :content
end

当我尝试加载图像时,这样说:

When I try to load an Image, say like this:

Image.first(
      :conditions => {:id => id},
      :include => :asset
)

它指定了两个疑问,一是要检索的图像,另一个获取资产(仅供参考,出现这种情况也是,如果我指定:加入)。根据我的理解,ActiveRecord的这样做,因为它不知道有一个图片和资产之间的一个一对一的关联。有没有办法强制加入,获取2对象一气呵成?我也试着使用连接使用自定义选择,但我最终不得不手工创建的ActiveRecord模型和它们的相关性。

It specifies two queries, one to retrieve the Image and another to retrieve the Asset (FYI, this happens also if I specify a :joins). Based on my understanding, ActiveRecord does this because it doesn't know there's a one-to-one association between Image and Asset. Is there a way to force a join and retrieve the 2 objects in one go? I've also tried using join with a custom select, but I end up having to create the ActiveRecord models manually and their associations.

是否ActiveRecord的提供一种方式来做到这一点?

Does ActiveRecord provide a way to do this?

推荐答案

(这是Rails 3的语法。)

(This is for Rails 3 syntax.)

MetaWhere是一个真棒宝石制作复杂的查询是ActiveRecord的通常的域名方便,红宝石般的外面。 (@wayne:支持外连接以及)

MetaWhere is an awesome gem for making complex queries that are outside of ActiveRecord's usual domain easy and ruby-like. (@wayne: It supports outer joins as well.)

https://github.com/ernie/meta_where

多态连接是有点麻烦。以下是我做矿用MetaWhere:

Polymorphic joins are a little trickier. Here's how I did mine with MetaWhere:

Image.joins(:asset.type(AssetModel)).includes(:asset)

其实,我在多态关联,上课做了一个方便的方法:

In fact, I made a convenience method in my polymorphic-association-having class:

  def self.join_to(type)
    joins(:asset.type(type)).includes(:asset)
  end

因此​​,它总是会查询及放大器;贪婪加载特定类型的资产。我还没有尝试过多种类型加入一个查询中混合了。由于使用相同的外键多态性作品引用不同的表,在SQL水平,你必须将其指定为独立的连接,因为一个连接只能连接到一个表。

So it will always query & eager load a particular type of asset. I haven't tried mixing it with multiple types of join in one query. Because polymorphism works using the same foreign key to reference different tables, on the SQL level you have to specify it as separate joins because one join only joins to one table.

这仅与ActiveRecord的3作品,因为你有机会到AREL的神奇的力量。

This only works with ActiveRecord 3 because you have access to the amazing power of AREL.

这篇关于在ActiveRecord的多态关联的预先加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆