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

»Recent entries" for water are NOT unique #7

Closed
janraasch opened this issue Dec 21, 2023 · 1 comment
Closed

»Recent entries" for water are NOT unique #7

janraasch opened this issue Dec 21, 2023 · 1 comment

Comments

@janraasch
Copy link

janraasch commented Dec 21, 2023

Hi (again) 😁😇,

while working on #2 / #6 I realised that the distinct(:amount)-call in WaterEntry

scope :recent, -> { distinct(:amount).order(created_at: :desc).limit(3) }

does not actually do anything. Here's a link to the documentation: https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-distinct. The method signature of distinct is very misleading I think 😅.

How to reproduce

Add a couple of WaterEntry-items with the same value:

Screenshot 2023-12-21 at 13 32 59

Idea 4 a Fix

IMHO the simplest thing would be do take care of uniqueness (and sorting - #2) after loading the latest three items into the application, i.e.

scope :recent, -> { order(created_at: :desc).limit(3).to_a.uniq(&:amount_value) }

or

scope :recent, -> { order(created_at: :desc).limit(3).to_a.uniq { |wi| wi.amount.value } }

Then, #2 could be solved by appending .sort_by(:amount).

@joemasilotti
Copy link
Owner

Ha, I should have read this first before addressing your other PR! 😆

This still won't work if the last 2 entries are the same because we are querying for 3 before grabbing unique entries.

I'm going to close this in favor of the PR you opened since that now has a failing test.

@joemasilotti joemasilotti closed this as not planned Won't fix, can't repro, duplicate, stale Dec 21, 2023
janraasch added a commit to janraasch/daily-log that referenced this issue Dec 22, 2023
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

No branches or pull requests

2 participants