1

This works! BUT if my JSON returns 200 programs, this will also print all those 200 programs. BUT I want to change the helper method in a way that it only return TOP 5 that their ercentage is the highest.

How Can I modify my helper method to achieve this?

P.S: I know probably the best and professional way of doing this is working with "scope" and "order",etc in the Model files or the ActiveRecord query interfaced that actually creates the JSON...but that is for now way out of my leauge! Wanted to get this done with baby steps...that for later...

2 Answers 2

2

try the following

def patient_counts(program)
  sorted = program.patient_counts.sort { |a, b| b.money <=> a.money }
  sorted[0..4]
end

UPDATE: limiting to just 5 records fetched from the db

def patient_counts(program)
  program.registry_patient_counts.limit(5).order('patient_count_percentage DESC')
end
Sign up to request clarification or add additional context in comments.

10 Comments

thanks man you helping me a couple time so far on SO but this method didn't change anything.
can you give more details about why it didn't work? i don't have any clue what program.patient_counts return but based on your code, it returns an array of patients which have a money method. did you get any errors?
I didn't get any error but for example I changed it to sorted[0..1] to see if it returns two things, but it still returned the three things that I have in DB. Give me five minutes and I will give more details with picture and code and JSON sample. Thanks a lot.
i'm not sure if it will make a difference (I'd be surprised if it did) but can you try my updated answer?
hey I created a sample app (so I can also try jbuilder :D and i'm lovin it). github.com/jvnill/jbuilder_test can you try to not use the helper? if you want you can edit the code on that app so we more or less have the same code but what's in that is working.
|
1

Create a hash with patients as the key, and patients.money as the value. This could be accomplished with something like:

unsorted_patients = { }
program.patient_counts.each do |patient|
    sorted_patients[patient] = patient.money
end

Then use sort_by to sort the hash into a 2d array:

sorted_patients = unsorted_patients.sort_by { |patient, money| money }
sorted_patients.reverse!

The first five elements of sorted_patients will now be 2-element arrays containing the five patients with the most money, and how much money they have, something like this:

[[<Bob>, 150000], [<Mary>, 138000], [<Joe>, 125000],...]

You should be able to easily extract the needed information from that.

3 Comments

how about the "top 5" part of it?
updated to explain that part too. It might be obvious, but just to be clear, <Bob>, etc., are placeholders for whatever type of object patient is.
I also updated my question with a full context of what I am asking, if you have a sec please take a look from the EDIT part in my question. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.