Reputation: 592
This is my first time using scope in rails. I'm trying to sort a list of chapters by a field called :priority
, an integer, in my Chapter Model. I have looked over the scope documentation but I cannot seem figure out how to get the feature to work.
Model
class Chapter < ActiveRecord::Base
belongs_to :book
scope :priority_sort, -> { order(priority: :asc) }
end
Controller
@chapters = Chapter.all.priority_sort
And the view
<% @book.chapters.each do |chapter| %>
<%= link_to chapter.title, [@book, chapter] %>
<% end %>
What the view currently looks like priority/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
What the view looks like with a 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
What am I missing here?
Upvotes: 0
Views: 24
Reputation: 10416
<% @book.chapters.each do |chapter| %>
<%= link_to chapter.title, [@book, chapter] %>
<% end %>
Is this a mistake? If not then it's because you're scoping on something in the controller you don't use. I.e
@chapters = Chapter.priority_sort.all
If not then you can change it to
<% @book.chapters.priority_sort.each do |chapter| %>
<%= link_to chapter.title, [@book, chapter] %>
<% end %>
Upvotes: 2