Question

Using Rails 4 and the latest Active Admin. I am trying to build an Active Admin index page for my Submission model. In short, it's a contest app where one submission has many scores. In the Score model, there is a field called :total_score where a judge inputs their score for that entry.

So naturally, to find the final score of the submission I need to average all children scores:

# In my Submission model
def calculate_final_score
  self.scores.average(:total_score)
end

Which then returns the averaged number. Naturally, I want to display this number in my Submission Active Admin index page. I use this code (it's a table-format index page):

column "Score", sortable: :calculate_final_score do |submission|
  submission.calculate_final_score
end

While it displays the value fine, it does not know how to sort this custom method. Like, if I tell it to sort by that column descending, it will order it 10.0, 6.0, 3.75, 8.0, 9.0, etc. How do I get it to sort properly?

Secondly, it also does not know how to filter this custom method column. I put in the code filter :calculate_final_score but it does not display that filter on the page at all. Is this a limitation of Active Admin or is there a way to make that filter work properly? Thanks for any help.

Était-ce utile?

La solution

this is a duplicate of the following questions:

nevertheless, those questions don't have a real answer to what's happening in active_admin and why this stuff is not working.

the reason for this can be found in ActiveAdmin::OrderClause.

it only supports columns on the model and columns on associated models.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top