夏普认证是什么意思
夏普认证是什么意思
最满意答案
通常,为了解决这些N + 1查询问题,我们使用
includes
和通过传递散列,我们可以includes
嵌套关联。 所以看这段代码,调用receipt.message.sender
会触发错误,所以我们有一个Receipt
模型,一个message
关联和一个关联的sender
。 因此,如果我们可以找到我们加载Receipt
我们可以像添加其他任何模型一样添加includes(message: :user)
。挖掘邮箱 gem,您的
show
action中的receipts_for
方法只是Mailboxer::Receipt
上几个范围的封装。 由于这个方法只是为你运行一些范围,我们可以链接到它的末尾,就好像它是一个正常的ActiveRecord
链。因此,考虑到这一切,我们应该能够添加我们的
includes
并避免N + 1查询问题,最终得到类似@receipts = conversation.receipts_for(current_user).includes(message: :sender)
Normally, to fix these N+1 query problems we use an
includes
and by passing a hash, we canincludes
nested associations. So looking at this code, callingreceipt.message.sender
is triggering the error, so we have aReceipt
model, amessage
association on it and asender
associated to that. So if we can find where we load theReceipt
we can addincludes(message: :user)
like we would for any other model.Digging into the mailboxer gem, the
receipts_for
method in yourshow
action is just a wrapper for a couple of scopes onMailboxer::Receipt
. Since this method just runs some scopes for you, we can chain onto the end of it as if it were a normalActiveRecord
where
chain.So with all that in mind, we should be able to add on our
includes
and avoid the N+1 query problem, ending up with something like@receipts = conversation.receipts_for(current_user).includes(message: :sender)
相关问答
更多-
通常,为了解决这些N + 1查询问题,我们使用includes和通过传递散列,我们可以includes嵌套关联。 所以看这段代码,调用receipt.message.sender会触发错误,所以我们有一个Receipt模型,一个message关联和一个关联的sender 。 因此,如果我们可以找到我们加载Receipt我们可以像添加其他任何模型一样添加includes(message: :user) 。 挖掘邮箱 gem,您的show action中的receipts_for方法只是Mailboxer::R ...
-
Bullet指向您检测到同一关联的多个调用的位置, 而不是指向应添加预先加载的位置。 在_list.html.erb模板中的某个位置,您可能会遍历您的RSVP(无论是什么),并且对于每个RSVP,您试图通过调用tickets关联来确定它是否有任何票证。 Bullet建议你添加include(:tickets) 到finder,它为你在模板中遍历的RSVP (可能是你的AcceptedRSVPs控制器中的某个地方) 设置变量 。 一旦完成,就不会有每个RSVP的SQL运行来查找它的票据,因此你将摆脱N + 1 ...
-
您需要在WHERE子句中告诉ActiveRecord您正在使用哪个表。 在大多数情况下,您可以使用这样的散列来定位关联的表: Post.includes(:tags).where(tags: { name: 'foo' }) 但是,当使用LIKE时,您需要创建一个字符串条件,在这种情况下,您只需指定表: class Post # don't use an argument with the same name as the method. # its confusing and can lead ...
-
为了更加清楚,让我更详细地描述细节: class User < ActiveRecord::Base has_many :submissions end class Submission < ActiveRecord::Base belongs_to :category belongs_to :user has_many :submission_details end class SubmissionDetail < ActiveRecord::Base belongs_to : ...
-
如何避免N + 1查询(How to avoid N+1 query)[2023-11-29]
您可以“急切加载”以避免N + 1查询,如下所示: user = User.includes(pricing_rules: :abilities).first Ability.includes(:pricing_rule).map do |a| user.pricing_rules.matching_ability(a).first || a.pricing_rule end 您应该在生成的SQL中看到这会LEFT OUTER JOIN您的查询添加LEFT OUTER JOIN ,因此ActiveRe ... -
协会的第一件事实在令人困惑。 我认为你正在调用两个层次的问题,所以改变这一行应该有效 questions = Question.where(:id => q_ids).includes(section: :section) 您正在访问q.section的父部分,因此我们也需要包含它 parent_section = q.section.section rescue nil 编辑 它变得更加混乱我不确定你是否应该这样做,但我认为这将解决问题 questions = Question.where(:id = ...
-
Grails N + 1查询(Grails N+1 query)[2023-07-15]
我尝试了几种组合,但结果相同 - 如果你看一下生成的SQL,原始查询中没有连接,所以它必须做额外的查询才能加载用户和角色。 其他人在这个域类中遇到了问题 - 看起来GORM中存在一个错误,其中包含由域类组成的复合键,或者类似的东西。 通常人们对HQL的解决方法感到满意,并且运气好,所以你会:) def usersByRole = UserRole.executeQuery( 'select ur from UserRole ur ' + 'left join fetch ur.user ' + ... -
class User < ApplicationRecord has_many :likes has_many :liked_orders, through: :likes, class_name: 'Order' def liked_orders_id @liked_orders_id ||= liked_orders.pluck(:id) end def liked_order?(order_id) liked_orders_id.include?(order_ ...
-
Django,避免N + 1查询(Django, avoid N+1 query)[2022-05-30]
你想要prefetch_related foos = Foo.objects.prefetch_related('rule__rule_constraint') 然后,您可以使用以下内容遍历查询集: for foo in foos: rule_constraints = foo.rule.ruleconstraint_set.all() 您可以使用select_related来获取rule从而进一步改进。 foos = Foo.objects.select_related('rule').pre ... -
正如您在查询中包含:messages ,您已加载所有消息,因此您不需要另一个查询来检查它是否未读以及是否所有会话消息都未读。 您可以创建检查这些状态的方法: def has_unread_messages?(user) unread = self.messages.collect {|m| m.read_at.nil? && m.receiver_id == user.id} !unread.empty? end @conversation.messages中的所有消息都已预加载。 更新 这是更新 ...