Returning an attributes in specific order based on number of related attributes in DataMapper, Sinatra?

StackOverflow https://stackoverflow.com/questions/20986300

  •  25-09-2022
  •  | 
  •  

Question

Sorry about that title, wasn't too sure how I could word it.

In a sinatra web app using datamapper, I am returning an array of instances of a model (named Polls) like so:

user.new_polls.to_json(:relationships => {:options => {:methods => [:votes]}})

Each Poll has many Options, each option has many Votes.

How would I be able to return the options in its respective poll, in order of descending votes?

Thank you.

Was it helpful?

Solution

You can create a method in your Poll model, called options_sorted_by_votes (or something similar) and then use

user.new_polls.to_json(:relationships => {:options_sorted_by_votes => {:methods => [:votes]}})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top