简体   繁体   English

具有STI的作用域的has_many超级慢

[英]has_many with a scope super slow with STI

class User < ApplicationRecord
  has_many :buckets, -> { order(ranked_row: :asc) }
  delegate :incomes, :fixed_costs, :financial_goals, to: :buckets

  ...
end

I have Buckets, that are STI'd. 我有STI桶。 If I add that scope to the has_many, my page takes forever on 9 records, and seems to be loading something that should be cached 如果我将该范围添加到has_many中,则我的页面将永久保留9条记录,并且似乎正在加载应缓存的内容

74s ...

If I remove the scope, all is well 如果我删除范围,一切都很好

3秒...

Any idea how the scope on the has_many is affecting the STI?? 知道has_many的范围如何影响STI? ranked_row has an index, but it's the same, regardless. rank_row有一个索引,但是无论如何都相同。 I am using active_model_serializers, but I'm not sure if there's a correlation. 我正在使用active_model_serializers,但不确定是否存在关联。

Update 更新资料

Definitely something with active_model_serializers. 肯定是带有active_model_serializers的东西。 ActiveModel::SerializableResource.new(user) is in the controller, and bogs down in the console, too. ActiveModel::SerializableResource.new(user)在控制器中,并且在控制台中也停滞不前。 I removed everything from the from the serializer, and calling the scoped has_many is the thing. 我从序列化程序中删除了所有内容,然后调用了作用域已定义的has_many。 I'll hit up github. 我会上github。

Code

https://gist.github.com/dudo/f25767f00c874842a005 https://gist.github.com/dudo/f25767f00c874842a005

That's the smallest bit of code I could get to cause the issue. 那是我可能导致问题的最小代码。 Again, It works fine without the scope on the has_many, and it also works with removing the percent_complete method from Bucket... that method doesn't look too nasty. 同样,在has_many上没有作用域的情况下,它也可以正常工作,并且还可以从Bucket中删除percent_complete方法...该方法看起来不太讨厌。 What could be in that included_transactions method that's bringing it to a crawl when a scope is present?? 当存在作用域时,将其爬网的included_transactions方法可能是什么?

When adding scope to a has_many you need to explicitly declare the inverse_of . 将范围添加到has_many时,您需要显式声明inverse_of

https://github.com/rails/rails/blob/7f18ea14c893cb5c9f04d4fda9661126758332b5/guides/source/4_1_release_notes.md https://github.com/rails/rails/blob/7f18ea14c893cb5c9f04d4fda9661126758332b5/guides/source/4_1_release_notes.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM