So I have a model called 'Project' with fields 'unique_id' and 'version' (as well as other irrelevant fields like name). The unique_id is unique to each project and each project has multiple versions ie.
unique_id | version
1 | 1
1 | 2
2 | 1
What I am trying to do is select the newest version of each project and add them to @projects
for use by my view (where each project is displayed).
I have no idea how to do this but my thought process was to use Projects.group(:unique_id).count
to get a hash of unique_id and count (aka the version) and use that information to pull each record via a loop. That seems clumsy to me. I also know there is a maximum function but that that won't pull the record, only the max value. Help?!
Group by the unique_id
and select the records with MAX
version:
Project.select('MAX(version), unique_id, id, name').group(:unique_id).all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With