【发布时间】:2014-11-26 02:11:16
【问题描述】:
目前我需要将 .CSV 文件导入我的应用程序。
我发现这段代码(由以前的开发人员制作)将用作导入器。但我不确定如何运行这个脚本。 /tasks 有什么不同吗?因为当我运行rake -T 时它没有显示。
require 'csv'
module Migrators
module Groups
class << self
def import!
Post.skip_callback :create, :before, :set_last_action_at
Post.skip_callback :create, :after, :create_memberhip_of_owner
CSV.foreach(Rails.root.to_s + '/db/lagacy/groups.csv', :headers => true) do |row|
group = Group.new(row.to_hash)
group.save(:validate => false)
end
# Update last_action_at to created_at
Group.where('last_action_at IS NULL').update_all('last_action_at = created_at')
Group.find_by_sql("SELECT setval('groups_id_seq', (SELECT MAX(id) FROM groups))")
Post.set_callback :create, :before, :set_last_action_at
Post.set_callback :create, :after, :create_memberhip_of_owner
end
end
end
end
【问题讨论】:
标签: ruby-on-rails ruby csv import