Skip to content

Instantly share code, notes, and snippets.

@wxianfeng
Created September 29, 2011 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wxianfeng/1250498 to your computer and use it in GitHub Desktop.
Save wxianfeng/1250498 to your computer and use it in GitHub Desktop.
acts_as_nested_set tables rebuild lft rgt
#!/usr/bin/env ruby
#
# 使用acts_as_nested_set的表lft,rgt数据错乱了,根据已有数据重新调整为正确的!
orgunits = Orgunit.all # cant use Orgunit.where("")
Orgunit.delete_all
orgunits.each do |orgunit|
parent = unless orgunit.parent_id.nil?
Orgunit.find_by_id(orgunit.parent_id)
else
nil
end
new_orgunit = Orgunit.new
new_orgunit.otype = orgunit.otype
new_orgunit.name = orgunit.name
new_orgunit.leader_id = orgunit.leader_id
new_orgunit.status = orgunit.status
new_orgunit.description = orgunit.description
new_orgunit.id = orgunit.id
new_orgunit.created_at = orgunit.created_at
new_orgunit.updated_at = orgunit.updated_at
if parent.nil?
new_orgunit.save
else
new_orgunit.save(:validate=>false)
new_orgunit.move_to_child_of(parent)
end
p "insert #{new_orgunit.id}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment