0
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. 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

74s...

If I remove the scope, all is well

3s...

Any idea how the scope on the has_many is affecting the STI?? ranked_row has an index, but it's the same, regardless. I am using active_model_serializers, but I'm not sure if there's a correlation.

Update

Definitely something with active_model_serializers. ActiveModel::SerializableResource.new(user) is in the controller, and bogs down in the console, too. I removed everything from the from the serializer, and calling the scoped has_many is the thing. I'll hit up github.

Code

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. What could be in that included_transactions method that's bringing it to a crawl when a scope is present??

Dudo
  • 4,002
  • 8
  • 32
  • 57
  • does the behavior change when you remove the delegation? i'd try to create a minimal set to reproduce this and report a bug http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#create-an-executable-test-case – phoet Feb 13 '16 at 12:44
  • doesn't appear so. I changed them to individual has_many, same thing. – Dudo Feb 13 '16 at 23:37
  • I think the problem can be in models/bucket.rb. Source or it did not happen. – gizmore Feb 14 '16 at 00:01
  • Have your source. I found the method that is causing the problem, but I have no idea why. – Dudo Feb 14 '16 at 00:25
  • what happens if you eager-load the bucket and user relations? – phoet Feb 15 '16 at 20:32
  • https://github.com/rails/rails/issues/23693 opened an issue with Rails, if anyone wants to follow along. – Dudo Feb 15 '16 at 22:44

1 Answers1

1

When adding scope to a has_many you need to explicitly declare the inverse_of.

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

Dudo
  • 4,002
  • 8
  • 32
  • 57