Fix for user and role relationship

Conflicts:
	lib/tasks/migrate.rake
This commit is contained in:
chris 2013-09-06 15:21:40 +08:00
parent 134b639465
commit 1857ad428c
2 changed files with 24 additions and 4 deletions

View File

@ -89,9 +89,9 @@ class User
var[:id].each do |id,val|
# binding.pry if id == '5052c5b22b5c49ab02000004'
if (val=="true")
self.roles = self.roles.reject{|t| t.id.to_s==id}
self.role_ids.reject!{|t| t.to_s == id}
elsif(val=="false")
self.roles << Role.find(id)
self.role_ids += Array(id)
end
end
end
@ -106,9 +106,9 @@ class User
# binding.pry if id == '5052c5b22b5c49ab02000004'
if ( self.roles.include?(@roid) == false or val=="true")
self.sub_roles = self.sub_roles.reject{|t| t.id.to_s==id}
self.sub_role_ids.reject!{|t| t.to_s == id}
elsif(val=="false")
self.sub_roles << SubRole.find(id)
self.sub_role_ids += Array(id)
end
end

View File

@ -326,4 +326,24 @@ namespace :migrate do
`mongo #{Mongoid.config.database.name} --eval "db.tags.remove({_type: {$ne: 'Tag'}})"`
end
task :add_missing_user_link => :environment do
User.all.each do |user|
user.role_ids.uniq!
user.sub_role_ids.uniq!
user.save
user.roles.each do |role|
unless role.user_ids.include?(user.id)
role.user_ids << user.id
role.save
end
end
user.sub_roles.each do |sub_role|
unless sub_role.user_ids.include?(user.id)
sub_role.user_ids << user.id
sub_role.save
end
end
end
end
end