- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的开发环境是Rails 4.1和postgresql
我有 3 个具有 has_many 关系的模型:
class Item < ActiveRecord::Base
has_many :item_parts
has_many :parts, through: :item_parts
end
class Part < ActiveRecord::Base
has_many :item_parts
has_many :items, through: :item_parts
end
class ItemPart < ActiveRecord::Base
belongs_to :item
belongs_to :part
end
item_parts 连接表有一个 unit_count 属性来跟踪项目包含多少部分。
当我在 View 中遍历特定项目的所有部分时,我还需要从连接表中获取 unit_count 值。
这给了我 n+1 查询问题。
我试图预先加载连接表:
item.parts.includes(:item_parts)
ItemPart Load (0.5ms) SELECT "item_parts".* FROM "item_parts" WHERE "item_parts"."part_id" IN (24, 12, 3, 26)
但是当我这样做之后:
part.item_parts.where(item_id: item.id).first.unit_count
我得到了每个部分的以下 sql 查询;预加载不起作用
ItemPart Load (0.5ms) SELECT "item_parts".* FROM "item_parts" WHERE "item_parts"."part_id" = $1 AND "item_parts"."item_id" = $2 [["part_id", 24], ["item_id", 18]]
有什么建议吗?
最佳答案
通过此链接,我找到了适合我的案例的解决方案:Rails Eager Loading and where clause
我不知道 Active Record 查询方法:选择 ( http://apidock.com/rails/v4.1.8/ActiveRecord/QueryMethods/select )
似乎我无法预先加载连接表,因为在包含 item_parts 之后,我在项目部分的迭代期间使用了 where 子句。
似乎在迭代期间每次使用 where 子句时都会对数据库进行新查询。
相反,select 方法不会每次都访问数据库,而是对加载到内存中的 Active Record 集合起作用。
所以我改变了这个:
item.parts.includes(:item_parts)
part.item_parts.where(item_id: item.id).first.unit_count
用这个:
item.parts.includes(:item_parts)
part.item_parts.select{|item_part| item_part.item_id == item.id}.first.unit_count
然后我有一个查询来加载 item_parts:
ItemPart Load (0.5ms) SELECT "item_parts".* FROM "item_parts" WHERE "item_parts"."part_id" IN (24, 12, 3, 26)
关于sql - rails eager load has_many 通过连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30999963/
显然,可以实现 Haskell,使其在不改变语言语义的情况下急切地求值。如果这是真的,如何处理无限数据结构? http://csg.csail.mit.edu/pubs/haskell.html Th
有没有什么方法可以像@ManagedBean(eager=true ) 来自 javax.faces 包? @Named @ApplicationScoped public Mail() { ...
使用@ApplicationScoped @Named @Eager,我的@EJB注入(inject)的@Stateless bean未正确实例化并评估为空。 我有一个@ApplicationScop
我必须删除两个分隔符之间的字符串,即 从“123XabcX321”我想要“123321”。 对于一个简单的案例,我很好: $_=<>; s/X(.*)X//; print; 但是,如果像“123Xab
我有一个对象层次结构,订单,联系人,地址: public class Order { public virtual Contact BillingContact { get; set; }
Eager 模式仅支持 TF native 优化器我在以下尝试过的每个优化器中都会遇到此错误: def create_model(): model = tf.keras.models.Sequ
我有这样的数据设计:交易属于授权,而授权又属于客户。或者用结构体表示: type Transaction struct { shared.Transaction // transaction
我想在 Scheme 中做一个懒惰的列表。这是我到目前为止。 ;; Constructor for Pairs (define (cons-stream a b) (cons a (λ() b))
今天是个好日子 我是 Laravel 的新手,目前正在用它构建我的第一个简单的新闻应用程序 我有一个简单的查询,使用 laravel 的预先加载功能 但是在这个急切的加载功能中,我想根据某些条件获取某
我正在制作一个动态表单,让用户使用 vue.js 自动生成带有游戏统计信息的图像,并使用 Keen-ui。我是 vue.js 的新手,过去两周开始学习它。 现在,我在使用 eager-ui 进行选择下
我 fork 了 eager/dashboards github 存储库,并尝试创建一个 Dockerfile 以在 Docker 容器中运行仪表板。 我的 fork :https://github.
我的实体: @Entity @NoArgsConstructor public class Company { @Id @Getter @GeneratedValue(strategy =
在我的实体中,我必须使用 fetch = FetchType.EAGER 因为我有一个错误: nested exception is org.hibernate.LazyInitializationE
我在 Java 中使用 play 2.0.4。我正在尝试从 play 提供的内存数据库中保存和检索值(Ebean 是底层 JPA 实现)。下面是一些示例代码: @NoobDisclaimer ("I'
在 Pro .NET Performance - Optimize Your C# Applications 的第 96 页上,它谈到了 GC 急切根收集: For each local variab
我无法从数据库加载对象及其相对映射对象。可能我的“ map 系统”是错误的。 在数据库中保存对象(及其相对映射对象)时,一切正常,因此对象似乎已正确映射。 Utente是我的“主”对象 publ
我需要使用急切查询在 Laravel 中执行子查询,但结果始终为空。 查询: $project = Log::where('project_id', $id)->with(['log_occurenc
我有这个程序: var planetStream = require('../../'); var app = require('http').createServer(handler); var i
我需要与FetchType.EAGER(下面的示例)建立关系,但我有异常(exception)。如果我将 FetchType.EAGER 更改为 FetchType.LAZY 一切正常,但我确实需要
我正在使用 EF6 并尝试急切获取对象的整个结构。问题是我正在使用继承。 假设我有这个类(class)。 数据库上下文 DbSet A { get; set; } 示例类 public class A
我是一名优秀的程序员,十分优秀!