![](/img/trans.png)
[英]rails to_xlsx Couldn't find all Vulnerabilities with 'id': (all, {}) (found 0 results, but was looking for 2)
[英]Rails find all order, found 0 results expected 2
我在根據投票列對表格進行排序時遇到了麻煩,投票是一個多值屬性,因此我使用count並嘗試根據該值對其進行排序。 這是代碼
@topics = Topic.find(:all,:order=>'@topic.votes.count DESC')
Rails返回一個錯誤,指出
ActiveRecord::RecordNotFound in TopicsController#index
Couldn't find all Topics with 'id': (all, {:order=>"@topic.votes.count DESC"}) (found 0 results, but was looking for 2)
剛開始使用Rails,但仍然有些困惑,您的幫助將不勝感激。
這是在更高版本的Rails中使用的Rails 2語法。
您可以執行以下操作:
Topic.joins(:votes).group('topics.id').order('count(topics.id) DESC')
最好不要選擇join,而是最好的選擇
Topic.include(:votes).group('topics.id').order('count(topics.id) DESC')
多虧了ahmed和snehal,我才能夠找到我想要的答案,這就是
Topic.left_outer_joins(:votes).group('topics.id').order('count(topics.id) DESC')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.