Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Switch
.count
to.size
where appropriate #1605Switch
.count
to.size
where appropriate #1605Changes from 4 commits
4a4a7d2
99ffa22
cefa9bb
5acab63
b7d3c15
fcee84f
c0e49b3
e603681
abeb3ce
22bacd9
85a2b47
8766859
2938902
279ef10
be52bcb
1b1e73d
beb53ec
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
@pellaea is this OK in
Query::HighLevelQueries
? At the point of figuring out thepaginator
and the@num_results
, the query is done, so it seemslength
is more appropriate.It may make no difference currently, but it will probably matter if Query starts using scopes.
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.
Ok i now think
size
may be more appropriate.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.
Oh dear god! I can't possibly read that entire email thread right now!
I do want to make sure one thing is pointed out in this one case, though --
ids
here is not a query, it's just a plain oldArray
. So there's no question of it executing SQL queries or anything if "done wrong".(Is there in fact any difference between size, count and length on a plain old Array??... oh hold on, strike that question... it's almost certainly answered somewhere in the email thread I just deleted! :)
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.
LOL
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 don't know if
length
is right; I don't know whether this is right. (I don't how where it gets used in user workflow.)Maybe use
size
here and in other places? Is that bad form?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.
@JoeCohen I don't know either, but I can answer one part of it. According to internet,
size
is an alias oflength
. (on Medium)(on Ruby in Rails).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.
Update: Sorry,
size
is NOT an alias oflength
. Thank you Stack Overflow!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.
However, that's an old answer. In the console, I notice that
length
on an association that has already been fetched only counts the number of cached records, so that may have changed.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.
After reading through that whole thread, i think I should go with
size
. Thanks for figuring that out.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.
You made me look.
If we need the records,
length
is better.Else
size
is better.So it looks like
size
is faster here. But I don't know if it's faster enough to be worth worrying about.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.
From Roy Lindauer
count on a collection will always execute a COUNT() SQL statement to get an accurate count value. size returns the size of the collection, but will execute a COUNT() SQL statement if it's not already loaded. length returns the size of the collection from memory. If the collection has already been loaded and is in memory length and size are equivalent.
Relying on what's in memory may give you inaccurate results as the database could be modified while you are working on that collection. But making a bunch of expensive database calls to get an accurate count is probably something you want to avoid. There may be times when a count is preferred, but when in doubt it's probably better to use length.
also see diagrams here: https://www.akshaykhot.com/rails-length-size-count-difference/
I'm going to post all this stuff in a GitHub Discussion for reference.
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.
Though it's not the point of this PR, can you use
one?
instead of
length == 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.
Yes! I don't know that method. Will alter
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.
Another place for
one?
, which is defined ashttps://github.com/rails/rails/blob/56bcc0abd3c9a6b09469e9428f6eea0dd77c2294/activerecord/lib/active_record/relation.rb#L282
Note: I'm fine with not using
one?
here and elsewhere. I'm just putting it out as an option.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.
Oops. Probably no on
size
, stick withlength
, asobservations
is used on the next line.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.
OK. I went with
one?
- If you get a chance please have another look over everything — i've changed everything in the PR — and check if it seems right.