使用child_id对模型进行排序

时间:2015-03-04 18:07:02

标签: ruby-on-rails ruby postgresql

我有型号

class Category < ActiveRecord::Base
  has_many :child, :class_name => 'Category', :foreign_key => 'category_id'
  belongs_to :parent, :class_name => 'Category'
  mount_uploader :photo, ImageUploader
end

并在db中有一些数据。例如:

> ID Name category_id
> 1 Moto null
> 2 Breaks 1
> 3 Car null
> 4 Tires 3
> 5 Brembo 2
> 6 Komo 4

而且我不知道如何为第一个父母和去孩子后排序模型。例如:

ID Name category_id 
1 Moto null 
2 Breaks 1 
5 Brembo 2
4 Car null 
5 Tires 4 
6 Komo 5

并将此数据插入

= f.collection_select(:category_id, @category, :id, :name, {:prompt=>false}, {class:'select', :multiple=>'multiple'})

0 个答案:

没有答案