【问题标题】:Returning an attributes in specific order based on number of related attributes in DataMapper, Sinatra?根据DataMapper,Sinatra中相关属性的数量以特定顺序返回属性?
【发布时间】:2014-01-08 11:37:22
【问题描述】:
对这个标题感到抱歉,我不太确定我该如何措辞。
在使用 datamapper 的 sinatra Web 应用程序中,我返回一个模型实例数组(名为 Polls),如下所示:
user.new_polls.to_json(:relationships => {:options => {:methods => [:votes]}})
每个投票都有很多选项,每个选项都有很多投票。
我如何能够按投票降序返回其各自民意调查中的选项?
谢谢。
【问题讨论】:
标签:
ruby
sinatra
datamapper
【解决方案1】:
您可以在 Poll 模型中创建一个名为 options_sorted_by_votes(或类似名称)的方法,然后使用
user.new_polls.to_json(:relationships => {:options_sorted_by_votes => {:methods => [:votes]}})