0

このRailsCastに従って、1つのモデルでうまく機能するアイテムのソート可能なリストを作成しましたが、結合モデルで整理されたアイテムをソートする必要があり、その方法がわかりません。次に例を示します。

プログラム内のすべてのサイクルを列ごとに整理したいと考えていcycle_orderます。

cycle_order列は表cycles_programsにあります

適切な尺度については、下部の結合テーブルの図を参照してください。

class Cycle 

  has_many :cycles_programs
  has_many :programs, :through => :cycles_programs

  accepts_nested_attributes_for :programs
  accepts_nested_attributes_for :cycles_programs, allow_destroy: :true

class CyclesProgram

  belongs_to :program
  belongs_to :cycle

class Program 

  has_many :cycles_programs
  has_many :cycles, :through => :cycles_programs

  accepts_nested_attributes_for :cycles
  accepts_nested_attributes_for :cycles_programs, allow_destroy: :true

スキーマは次のとおりです。

create_table "programs", :force => true do |t|
    t.string   "name"
    t.datetime "created_at",    :null => false
    t.datetime "updated_at",    :null => false
  end

create_table "cycles_programs", :force => true do |t|
    t.integer  "program_id"
    t.integer  "cycle_id"
    t.integer  "cycle_order"
    t.datetime "created_at",  :null => false
    t.datetime "updated_at",  :null => false
  end

create_table "cycles", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

ここに画像の説明を入力

4

2 に答える 2

0

そして答えはとてもシンプルです。モデルに追加default_scope -> { order(:cycle)order) }するだけCyclesProgram

于 2013-06-28T16:51:54.143 に答える