Created
September 29, 2011 10:41
-
-
Save wxianfeng/1250498 to your computer and use it in GitHub Desktop.
acts_as_nested_set tables rebuild lft rgt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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