この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