-
Notifications
You must be signed in to change notification settings - Fork 26
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
#1605
Changes from 5 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
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,7 @@ def name_with_most_observations(names) | |
best_name = nil | ||
best_count = -1 | ||
names.each do |name| | ||
count = name.observations.count | ||
count = name.observations.length | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update: Sorry, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, that's an old answer. In the console, I notice that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After reading through that whole thread, i think I should go with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You made me look.
So it looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From Roy Lindauer 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.
nimmolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
next if count <= best_count | ||
|
||
best_name = name | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,7 +100,7 @@ def update_pref(pref, val) | |
def update_content_filter(pref, val) | ||
filter = ContentFilter.find(pref) | ||
@user.content_filter[pref] = | ||
if filter.type == :boolean && filter.prefs_vals.count == 1 | ||
if filter.type == :boolean && filter.prefs_vals.length == 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though it's not the point of this PR, can you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! I don't know that method. Will alter
nimmolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val == "1" ? filter.prefs_vals.first : filter.off_val | ||
else | ||
val.to_s | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -350,7 +350,7 @@ def figure_out_where_to_go_back_to | |
@back_object = Observation.safe_find(@back) | ||
return if @back_object | ||
|
||
@back_object = if @collection_number.observations.count == 1 | ||
@back_object = if @collection_number.observations.length == 1 | ||
nimmolo marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another place for # File activerecord/lib/active_record/relation.rb, line 282
def one?
return super if block_given?
limit_value ? records.one? : size == 1
end Note: I'm fine with not using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops. Probably no on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. I went with |
||
@collection_number.observations.first | ||
else | ||
@collection_number | ||
|
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