首页 \ 问答 \ 夏普认证是什么意思

夏普认证是什么意思

夏普认证是什么意思
更新时间:2021-12-16 19:12

最满意答案

通常,为了解决这些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 can includes nested associations. So looking at this code, calling receipt.message.sender is triggering the error, so we have a Receipt model, a message association on it and a sender associated to that. So if we can find where we load the Receipt we can add includes(message: :user) like we would for any other model.

Digging into the mailboxer gem, the receipts_for method in your show action is just a wrapper for a couple of scopes on Mailboxer::Receipt. Since this method just runs some scopes for you, we can chain onto the end of it as if it were a normal ActiveRecord 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查询,如下所示: 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 = ...
  • 我尝试了几种组合,但结果相同 - 如果你看一下生成的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_ ...
  • 你想要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中的所有消息都已预加载。 更新 这是更新 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)