Browse files

fix import data

  • Loading branch information...
1 parent 848dcc2 commit 2a132e53153613a544b65236c92cad6c28105a72 @ck3g committed Mar 30, 2013
Showing with 18 additions and 8 deletions.
  1. +2 −0 app/models/legacy/base.rb
  2. +5 −4 app/models/legacy/comment.rb
  3. +10 −3 app/models/legacy/user.rb
  4. +1 −1 lib/tasks/import.rake
View
2 app/models/legacy/base.rb
@@ -1,4 +1,6 @@
class Legacy::Base < ActiveRecord::Base
self.abstract_class = true
establish_connection 'sc2_md'
+
+ LOG_FILE = "#{Rails.root}/log/import.log"
end
View
9 app/models/legacy/comment.rb
@@ -3,9 +3,10 @@ class Legacy::Comment < Legacy::Base
def import(article)
user = User.where(legacy_id: self.author_id).first
- article.comments.new(comment: self.body, ip_address: self.ip_address)
- article.user_id = user.try(:id)
- article.created_at = self.created_at
- article.save
+ comment = article.comments.new(comment: self.body,
+ ip_address: self.ip_address)
+ comment.user_id = user.try(:id)
+ comment.created_at = self.created_at
+ comment.save!
end
end
View
13 app/models/legacy/user.rb
@@ -3,14 +3,21 @@ class Legacy::User < Legacy::Base
def import(membership)
pass = rand(10000) + 100000
- user = ::User.where(email: membership.Email.downcase).first_or_initialize
+ user = ::User.where(email: membership.LoweredEmail).first_or_initialize
user.password = pass
user.password_confirmation = pass
user.created_at = membership.CreateDate
user.legacy_id = membership.UserId
user.username = self.UserName
- user.save
+ if user.valid?
+ user.save
+ else
+ log = Logger.new(LOG_FILE)
+ log.error "\nUser not imported. Membership: #{membership.inspect}"
+ log.error "ror user: #{user.inspect}"
+ log.error "errors: #{user.errors.messages}"
+ end
- user.reload
+ user
end
end
View
2 lib/tasks/import.rake
@@ -14,7 +14,7 @@ namespace :import do
if legacy_user.present? && legacy_profile.present?
user = legacy_user.import(membership)
- legacy_profile.import(user.profile)
+ legacy_profile.import(user.profile) if user.persisted?
end
print "\r#{index + 1} of #{total_users} imported"
end

0 comments on commit 2a132e5

Please sign in to comment.