Arel
Arel

Reputation: 3938

How do I order items in a join table?

I just can't figure this out.

In the show.html.erb I want to order items in a join model by a field in that join model.

e.g.

Show.html.erb

<% @cycle.cycles_groups.each do |cycle| %>
  <%= cycle.group.name %>
<% end %>

I want to order the groups by the group_order field in the cycles_groups table

class Cycle

has_many :cycles_groups
has_many :groups, :through => :cycles_groups

class Group

has_many :cycles_groups
has_many :cycles, :through => :cycles_groups

class CyclesGroup

 belongs_to :cycle
 belongs_to :group

Upvotes: 0

Views: 64

Answers (1)

Pedro Nascimento
Pedro Nascimento

Reputation: 13926

Add a default scope to CyclesGroup

default_scope -> { order(:group_order) }

Upvotes: 2

Related Questions