我按照这个 RailsCast制作了一个可排序的项目列表,这对一个模型非常有用,但是我需要对由连接模型组织的项目进行排序,并且不知道如何去做。这是一个例子:
我想按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