Skip to content
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

Merged
merged 1 commit into from
Apr 28, 2017
Merged

[B] Fix resource count for collections #290

merged 1 commit into from
Apr 28, 2017

Conversation

SMaxOwok
Copy link
Member

[Fixes #287]

@SMaxOwok SMaxOwok added the Status: Review Needed Pull request needs to be reviewed. label Apr 24, 2017
@zdavis
Copy link
Member

zdavis commented Apr 25, 2017

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.

@zdavis zdavis added Status: Revision Needed Pull request has been reviewed and needs to be revised before it can be accepted. and removed Status: Review Needed Pull request needs to be reviewed. labels Apr 25, 2017
@SMaxOwok SMaxOwok force-pushed the bug/resource-count branch 2 times, most recently from 2c263a7 to df7d868 Compare April 25, 2017 22:36
@SMaxOwok SMaxOwok added Status: Review Needed Pull request needs to be reviewed. and removed Status: Revision Needed Pull request has been reviewed and needs to be revised before it can be accepted. labels Apr 25, 2017
@SMaxOwok SMaxOwok force-pushed the bug/resource-count branch 2 times, most recently from d7a9a03 to 49ec805 Compare April 25, 2017 22:59

execute <<-SQL.squish
UPDATE collections
SET collection_resources_count = (SELECT count(1)
Copy link
Member

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:

http://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-reset_counters

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.

Copy link
Member Author

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!

Copy link
Member

@zdavis zdavis left a 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.

@zdavis zdavis added Status: Revision Needed Pull request has been reviewed and needs to be revised before it can be accepted. and removed Status: Review Needed Pull request needs to be reviewed. labels Apr 26, 2017
@SMaxOwok SMaxOwok added ready to accept and removed Status: Revision Needed Pull request has been reviewed and needs to be revised before it can be accepted. labels Apr 26, 2017
@zdavis zdavis merged commit e844ce8 into master Apr 28, 2017
@zdavis zdavis deleted the bug/resource-count branch April 28, 2017 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants