2015-07-18 35 views

回答

2

假設你:年齡字段是一個整數,你需要通過命令:FIRST_NAME它是這樣的:

Employee.where('age < ?', 60).order(:first_name) 
0

我會做這樣的:

#Controller 
@employees = Employee.sort_by_name('first_name ASC').young_employees 

#model 
scope :sort_by_name, ->(criteria) { order: criteria } 
scope :young_employes, -> { where("age < ?", 60)} 

爲了這個工作,請檢查您的「年齡」欄是整型的,也是你的名字列被稱爲「FIRST_NAME」

+1

儘管將一些邏輯提取到作用域是一個好主意,但這裏的命名選擇是非常殘酷的。 –

+1

lol yea,在編程時,我可怕的命名事物,以及我只花了2分鐘寫出答案,所以我沒有花太多時間考慮名稱,任何建議?讓我知道我會編輯答案。問候! – SsouLlesS