2016-11-25 78 views
0

这是在轨应用范围,我的第一次。我试图在我的章节模型中通过名为:priority(一个整数)的字段对章节列表进行排序。我已经查看了范围文档,但我似乎无法弄清楚如何让功能工作。轨道4范围不正确排序

模型

class Chapter < ActiveRecord::Base 
belongs_to :book 
scope :priority_sort, -> { order(priority: :asc) } 
end 

控制器

@chapters = Chapter.all.priority_sort 

和视图

<% @book.chapters.each do |chapter| %> 
    <%= link_to chapter.title, [@book, chapter] %> 
<% end %> 

的观点看什么看法目前看起来像 优先/ CHAPTER_TITLE

-15 
    About the authors 

    3 
    Chapter 18 Equal pay 

    -13 
    Chapter 2 Overview 

    -4 
    Chapter 11 Non-exempt employees: determining work time 

    -11 
    Chapter 4 Workers not covered by the FLSA 

是什么样的一个default_scope { order("priority ASC") }

-15 
    About the authors 

    -14 
    Chapter 1 Snapshot 

    -13 
    Chapter 2 Overview 

    -12 
    Chapter 3 Covered employers 

    -11 
    Chapter 4 Workers not covered by the FLSA 

缺少什么我在这里?

回答

2
<% @book.chapters.each do |chapter| %> 
<%= link_to chapter.title, [@book, chapter] %> 
<% end %> 

这是一个错误?如果不是这样,那是因为你在控制器中的某些东西上没有使用。即

@chapters = Chapter.priority_sort.all

如果没有,那么你可以把它改成

<% @book.chapters.priority_sort.each do |chapter| %> 
    <%= link_to chapter.title, [@book, chapter] %> 
<% end %>