完全工作
这可以改进,但我只做了你想要的
gem 'ancestry'
组.rb
class Group < ActiveRecord::Base
attr_accessible :parent_id, :name
has_many :people
attr_accessor :relationship
has_ancestry
def people_group_children
(people + children).sort_by(&:created_at)
end
RELATIONSHIP = ['parent', 'child', 'grandchild']
def relationship_by_depth(depth_diff)
@relationship = RELATIONSHIP[depth_diff] || (depth_diff-2) * 'great ' + RELATIONSHIP[2]
end
def arrange_as_desired(ancestor_depth = 0, parent_depth = 0)
output = '<ul>'.html_safe
output.safe_concat name + '(' + relationship_by_depth(depth - ancestor_depth) + ')'
people_group_children.map do |child|
output.safe_concat '<li>'
output.safe_concat child.arrange_as_desired(ancestor_depth, depth)
output.safe_concat '</li>'
end
output.safe_concat '</ul>'
output
end
end
人.rb
class Person < ActiveRecord::Base
attr_accessible :group_id, :name
belongs_to :group
attr_accessor :relationship, :depth
def parent; group; end
def people_group_children; []; end
RELATIONSHIP = ['parent', 'child', 'grandchild']
def relationship_by_depth(depth_diff)
@relationship = RELATIONSHIP[depth_diff] || 'great ' * (depth_diff-2) + RELATIONSHIP[2]
end
def arrange_as_desired(ancestor_depth = 0, parent_depth = 0)
output = '<ul>'.html_safe
@depth = parent_depth + 1
output.safe_concat name + "(#{relationship_by_depth(depth - ancestor_depth)})"
people_group_children.map do |child|
output.safe_concat '<li>'
output.safe_concat child.arrange_as_desired(ancestor_depth, depth)
output.safe_concat '</li>'
end
output.safe_concat '</ul>'
output
end
end
组\show.html.erb
<ul>
<%= raw @group.arrange_as_desired %>
</ul>
迁移
create_groups.rb
class CreateGroups < ActiveRecord::Migration
def change
create_table :groups do |t|
t.string :name
t.string :ancestry
t.timestamps
end
end
end
create_people.rb
class CreatePeople < ActiveRecord::Migration
def change
create_table :people do |t|
t.integer :group_id
t.string :name
t.timestamps
end
end
end