Tag: eager loading

Rails 3拒绝急于加载

我在Rails 3.0.7中工作。 我有一些多对多和一些一对多的关联,无法加载。 我的协会是: Person has_many :friends Person has_many :locations through=> :location_histories Location belongs_to :location_hour Location_hour has_many :locations 在我的控制器中,我有以下内容: @people = Person.includes([[:locations=>:location_hour],:friends]).where(“(location_histories.current = true) AND (people.name LIKE ? OR friends.first_name LIKE ? OR friends.last_name LIKE ? OR (friends.first_name LIKE ? AND friends.last_name LIKE ?))”).limit(10).all 然后在我看来,我有: locations.current是一个定义为: scope :current, lambda { where(“location_histories.current = ?”, true) } […]

如何在1次呼叫中急切加载两级关联?

我有FamilyTree, Node, Comment, & User模型。 这种关系是这样的: FamilyTree class FamilyTree < ActiveRecord::Base belongs_to :user has_many :memberships, dependent: :destroy has_many :members, through: :memberships, source: :user, dependent: :destroy has_many :nodes, dependent: :destroy end Node class Node < ActiveRecord::Base belongs_to :family_tree belongs_to :user has_many :comments, dependent: :destroy end Comment class Comment < ActiveRecord::Base belongs_to :user belongs_to :node end User […]

在模式中的两条路径上导航关系

从上到下,我的表之间有belongs_to关系,而另一方面则有has_many。 ReportTarget Report Manager Organization 和 Score Manager Organization 所以请注意, Report表和Score表是同一级别的。 他们都有Manager表作为他们的父母。 单独地,我可以弄清楚如何通过急切加载来导航它们。 首先,我会做: @blah = Organization.includes(managers: { reports: :report_targets }).find(params[:id]) 而对于第二个,我可以这样做: @blah = Organization.includes([managers: :scores]).find(params[:id]) 但是因为我在我的控制器中做了并且想要将JSON传递给JBuilder,我不知道如何传递它们? 或者将它们组合在一起? 这样产生的散列会将它们放在一个散列中,但使用单独的键: { “firstoneinfo” : [ # stuff that first json returns, can have their own internal hashes ], “SecondOneinfo : [ #stuff that second json returns, can […]

与Arel(Rails 3)的加载关联计数

简单的任务:鉴于文章有很多评论,能够在很长的文章列表中显示每篇文章有多少评论。 我正在尝试研究如何使用Arel预加载此数据。 README文件的“复杂聚合”部分似乎讨论了这种情况,但它并不完全提供示例代码,也没有提供在两个查询而不是一个连接查询中执行此操作的方法,这对于性能。 鉴于以下内容: class Article has_many :comments end class Comment belongs_to :article end 我如何预装文章设置每个评论有多少?

即使在急切加载之后,belongs_to关联也会单独加载

我有以下协会 class Picture < ActiveRecord::Base belongs_to :user end class User < ActiveRecord::Base has_many :pictures end 在我的PicturesController中,我渴望加载用户 class PicturesController < ApplicationController def index @pictures = Picture.includes(:user).all end end 在我看来,我正在显示每个图片的用户名 -@pictures.each do |picture| %tr %td= picture.name %td= picture.user.name 现在的问题是,即使我急于加载用户,我也会看到在视图中显示用户名时触发了各个查询。 我有大约1000张图片记录,我为此请求触发了1000多个查询。 我究竟做错了什么? 如何避免用户的个别查询?

Rails 4 Eager load limit子查询

有没有办法在急切加载时避免n + 1问题,并对子查询应用限制? 我想避免这样的大量SQL查询: Category.all.each do |category| category.posts.limit(10) end 但是我也希望每个类别只能获得10个post,所以标准的热切加载会获得所有post,但这还不够: Category.includes(:posts).all 解决这个问题的最佳方法是什么? N + 1是限制每个类别的post数量的唯一方法吗?

Rails添加自定义的热切加载

我在Rails中有许多自定义的find_by_sql查询。 我想用它们急切加载,但似乎没有一个好方法来做到这一点。 我已经看到eager_custom.rb文件浮动,现在它似乎不适用于Rails。 现在看来Rails确实急于加载,使用2个查询(常规查询加上查询,其中’id IN’来自第一个查询的ID),而不是过去使用的单个连接查询。 我的问题是,如果我执行自定义SQL查询,然后执行’id IN’查询,有没有办法将关联对象添加回初始查询结果? 例如,我有使用find_by_sql加载的主题,然后我找到主题ID在主题ID中的主题图像,有没有办法手动将图像添加回主题? 谢谢

如何急切加载与current_user的关联?

我在我的Rails应用程序中使用Devise进行身份validation。 我想在我的一些控制器中急切加载一些用户关联的模型。 像这样的东西: class TeamsController < ApplicationController def show @team = Team.includes(:members).find params[:id] current_user.includes(:saved_listings) # normal controller stuff end end 我怎样才能做到这一点?

为什么这个rails关联在急切加载后单独加载?

我试图通过急切加载避免N + 1查询问题,但它无法正常工作。 相关模型仍在单独加载。 以下是相关的ActiveRecords及其关系: class Player < ActiveRecord::Base has_one :tableau end Class Tableau :tableau_cards end Class TableauCard :card end class DeckCard :tableau_cards end class Card < ActiveRecord::Base has_many :deck_cards end class Turn < ActiveRecord::Base belongs_to :game end 我正在使用的查询是在Player的这个方法中: def tableau_contains(card_id) self.tableau.tableau_cards = TableauCard.find :all, :include => [ {:deck_card => (:card)}], :conditions => [‘tableau_cards.tableau_id = […]

你如何急切加载限制?

在热切加载的文档中,声明: 如果您急切地使用指定的:limit选项加载关联,它将被忽略,返回所有关联的对象: class Picture ‘Comment’, :order => ‘id DESC’, :limit => 10 end Picture.find(:first,:include =>:most_recent_comments).most_recent_comments#=>返回所有相关注释。 如果是这种情况,那么实现加载“限制”的最佳方法是什么? 假设我们急切地将最近10篇博客post加载到博客的首页上,我们显然不希望它们都是如此,那么是否应该指定post集合的限制和排序? 除此之外,可以在深度加载的元素上指定相同的条件 – 例如,仅在每篇博客文章中显示前三个评论吗? Blog.find(:blog_id, :include => {:posts => :comments } )