-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[B] Fix resource count for collections #290
Conversation
Good catch, however, I don't think the total count should be derived by counting collection.relationships.resources. That approach assumes that we'll always want to return all related resources from the API, which will probably not always be the case. For example, if a collection has, say, 500 resources in it, we're going to have to need to paginate it. We haven't solved for that yet, but we inevitably will at some point because we'll run into problems when we try to serialize large resource sets. The total count of resources in a collection should be returned by the API as an attribute on the collection, similar to how we return the resource count on the project. If we don't have it already, we'll need a counter_cache column on the collection, and we just included that in the serialized attributes. The cache is important so we don't end up with N+1 count queries. |
2c263a7
to
df7d868
Compare
d7a9a03
to
49ec805
Compare
|
||
execute <<-SQL.squish | ||
UPDATE collections | ||
SET collection_resources_count = (SELECT count(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that I tell you to avoid using models in migrations, and that's still true. However, there is a rails method for what you're doing here, which is appropriate in this case:
Resets one or more counter caches to their correct value using an SQL count query. This is useful when adding new counter caches, or if the counter has been corrupted or modified directly by SQL.
We should use the more idiomatic rails method, IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I had seen that method, but was trying to stick to SQL. Will update!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the idiomatic reset_counters method, then should be good to accept.
49ec805
to
ca4dff6
Compare
[Fixes #287]