2015-11-05 65 views
1

我想根據軌道上ruby欄的最小數據搜索模型。 特別是,我想搜索得分最高的答案數據。 我一直在執行如下,但它並不簡單,因爲它使用數組。 你能告訴不使用數組的方法嗎?基於軌道欄最小數據的搜索模型

#Answer_model 
id:Int 
name:string 
point:Int 

#/app/model/answer.rb 
class User < ActiveRecord::Base 
    scope :highest,-> { 
    order("point ASC") 
    } 
end 

#/app/controllers/answer_controllers.rb 
@user = Answer.highest[0] 

回答

1

highest返回ActiveRecord::Relation,而您不能打電話[]。而是選擇使用first的第一個元素,如下所示:

@user = Answer.highest.first