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

Sort "Recent entries" for water #2

Open
joemasilotti opened this issue Dec 17, 2023 · 5 comments
Open

Sort "Recent entries" for water #2

joemasilotti opened this issue Dec 17, 2023 · 5 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@joemasilotti
Copy link
Owner

Currently the "Recent entries" for water grabs the last 3 unique amounts. But I don't know how it sorts them. I prefer to see them sorted with the lowest amount on the left and the highest on the right.

This requires a fancier query than what is being used right now.

@joemasilotti joemasilotti added help wanted Extra attention is needed enhancement New feature or request labels Dec 17, 2023
@aniketpatidar
Copy link

To sort the "Recent entries" by the lowest amount on the left and the highest on the right, you can modify the query like this:

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

This will first order by the amount in ascending order and then by created_at in descending order, giving you the desired sorting for the recent entries.

@joemasilotti
Copy link
Owner Author

Hmm, that doesn't seem to work with Postgres.

PG::UndefinedColumn: ERROR:  column "amount" does not exist
LINE 1: ...es" WHERE "water_entries"."user_id" = $1 ORDER BY "amount" A...

@aniketpatidar
Copy link

The error indicates that the "amount" column is not recognized in the query, and this might be due to the use of distinct(:amount) causing a conflict with the subsequent ordering.

To address this issue, you can try using a subquery to first select distinct amounts and then order the results. Here's an adjusted version:

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

This should order the results by the lowest amount on the left and the highest on the right, without causing the "column does not exist" error.

@joemasilotti
Copy link
Owner Author

That doesn't change anything. It generates the same SQL.

[2] pry(main)> WaterEntry.distinct(:amount).order(amount: :asc, created_at: :desc).limit(3).to_sql
=> "SELECT DISTINCT \"water_entries\".* FROM \"water_entries\" ORDER BY \"amount\" ASC, \"water_entries\".\"created_at\" DESC LIMIT 3"
[2] pry(main)> WaterEntry.order(amount: :asc, created_at: :desc).distinct(:amount).limit(3).to_sql
=> "SELECT DISTINCT \"water_entries\".* FROM \"water_entries\" ORDER BY \"amount\" ASC, \"water_entries\".\"created_at\" DESC LIMIT 3"

I appreciate the help but I think it would be easier if you submitted a working PR if possible!

@aniketpatidar
Copy link

It seems the current approach doesn't alter the SQL as expected. I'll take your feedback to heart and work on a pull request to address this directly. Thanks for your patience and understanding! 🛠️

janraasch added a commit to janraasch/daily-log that referenced this issue Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
Status: Todo
Development

No branches or pull requests

2 participants