急切的加载未加载到导轨中

Posted

技术标签:

【中文标题】急切的加载未加载到导轨中【英文标题】:Eager loading not loading in rails 【发布时间】:2012-09-03 01:06:37 【问题描述】:

嗨,我在这里遗漏了什么吗?我阅读了有关在加载时包含连接的文档,并尝试了以下操作。您可以看到,当我使用“包含”时,包含的联接不会输出所需的数据,但是当我在事后显式调用联接模型时,您可以看到有所需的数据。以下是我在 IRB 中的模型和行为:

class Appearance < ActiveRecord::Base
  belongs_to :photo
  belongs_to :person
end


class Photo < ActiveRecord::Base
  belongs_to :event
  has_many :appearances, order: 'position'
  has_many :people, :through => :appearances
end


class PhotosController < ApplicationController
  def index
    @event            = Event.find(params[:event])
    @photos           = @event.photos.includes(:people, :appearances)
    # this does not show any appearance records, however this does
    # @photos           = @event.photos.first.people
  end
end

#IN IRB
 Photo.find(51941, :include => [:people, :appearances])
  Photo Load (0.4ms)  SELECT `photos`.* FROM `photos` WHERE `photos`.`id` = 51941 LIMIT 1
  Appearance Load (0.3ms)  SELECT `appearances`.* FROM `appearances` WHERE `appearances`.`photo_id` IN (51941) ORDER BY position
  Person Load (0.2ms)  SELECT `people`.* FROM `people` WHERE `people`.`id` IN (12103)

 => #<Photo id: 51941, time_created: "165732-0500", copyright_notice: "", country_primary_location_name: "", date_created: "20110119", created_at: "2011-01-20 07:33:24", updated_at: "2011-01-20 21:17:41", event_id: 602, position: 102, visible: nil, filename: "RAS_4824.JPG", folder: "", image: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil> 

1.9.3p194 :036 > Photo.find(51941).appearances
  Photo Load (0.4ms)  SELECT `photos`.* FROM `photos` WHERE `photos`.`id` = 51941 LIMIT 1
  Appearance Load (0.4ms)  SELECT `appearances`.* FROM `appearances` WHERE `appearances`.`photo_id` = 51941 ORDER BY position
 => [#<Appearance id: 73654, photo_id: 51941, person_id: 12103, created_at: "2011-01-20 07:33:24", updated_at: "2011-01-20 07:33:24", position: 1>] 

【问题讨论】:

【参考方案1】:

我认为你误解了includes 的意思。使用includes(或查找include 选项)告诉rails 在加载父节点的同时从数据库加载关联。这发生在您的 IRB 会话中,从两行可以看出:

Appearance Load (0.3ms)  SELECT `appearances`.* FROM `appearances` WHERE `appearances`.`photo_id` IN (51941) ORDER BY position
Person Load (0.2ms)  SELECT `people`.* FROM `people` WHERE `people`.`id` IN (12103)

当您将find 的结果输出到控制台时,您没有看到相关记录的事实只是inspect 方法的结果,它不会(默认情况下) 向您显示所有关联的数据(通常是大量数据)。

当您输入Photo.find(51941).appearances 时,您检查的不是照片而是外观关联(一个数组),这就是您可以在 IRB 控制台中看到结果的原因。

如果您想测试include 是否正常工作,请先将照片分配给一个变量:

photo = Photo.find(51941, :include => [:people, :appearances])
Photo Load (0.4ms)  SELECT `photos`.* FROM `photos` WHERE `photos`.`id` = 51941 LIMIT 1
Appearance Load (0.3ms)  SELECT `appearances`.* FROM `appearances` WHERE `appearances`.`photo_id` IN (51941) ORDER BY position
Person Load (0.2ms)  SELECT `people`.* FROM `people` WHERE `people`.`id` IN (12103)

您应该看到的 SQL 语句不仅适用于照片,还适用于相关的人物和外表。这表明 rails 已经预先加载了这些关联。

如果您随后获得了与 photo.appearances 的关联,并且您应该没有看到任何 SQL 查询,因为关联已经从数据库加载:

1.9.3p194 :036 > photo.appearances
=> [#<Appearance id: 73654, photo_id: 51941, person_id: 12103, created_at: "2011-01-20 07:33:24", updated_at: "2011-01-20 07:33:24", position: 1>] 

如果你没有使用include,你会在结果之前看到一个SQL查询:

1.9.3p194 :036 > photo.appearances
Appearance Load (0.3ms)  SELECT `appearances`.* FROM `appearances` WHERE `appearances`.`photo_id` IN (51941) ORDER BY position
=> [#<Appearance id: 73654, photo_id: 51941, person_id: 12103, created_at: "2011-01-20 07:33:24", updated_at: "2011-01-20 07:33:24", position: 1>] 

【讨论】:

啊,是的,我明白了。我的问题是我希望 JSON 中的所有数据都准备好并方便使用。我最终做的是:photos.to_json(:include => :appearances => :include => :person ) 感谢您的帮助!

以上是关于急切的加载未加载到导轨中的主要内容,如果未能解决你的问题,请参考以下文章

DJANGO:静态文件未在实时服务器中加载(但在本地加载)?

图像未在 XAMPP 中加载

如何在VS中加载Chart控件

启动图像未在 iPhone 应用程序中加载

Chrome 扩展程序未在 YouTube 的浏览器导航中加载

转到主页不会在Polymer中加载页面