我有一个来自下方控制器的片段,该片段应该按照从最新到最旧的创建日期排序调查列表。
# GET /surveys
def index
@survey_type = get_survey_type
@surveys = @survey_type.surveys
@surveys.order('created_at desc')
end
出于某种原因,无论是添加还是删除'desc'部分,排序顺序都不会改变。我做了一些
Survey.all.order('created_at')
和
Survey.all.order('created_at desc')
在Rails控制台中进行快速测试,订单按预期工作。所以在这个片段的某个地方可能就是问题所在。
答案 0 :(得分:1)
你应该:
@surveys = @surveys.order('created_at DESC')
或:
@surveys = @survey_type.surveys.order('created_at DESC')
您遇到此问题是因为您没有将@surveys
设置为ActiveRecord::Relation
作用域的实例。