2014-09-30 86 views
1

目前,我有一個ActiveAdmin頁面從一個CSV文件中獲取數據來填充數據庫表(用戶)。該表有一個唯一的標識符,它不是關係中使用的ID(用戶友好的代碼供用戶查看)。 該頁通過「active_admin_import」執行此操作紅寶石 - ActiveAdmin CSV自定義導入

現在,我想要填充另一個表(user_paths)的同樣的事情,問題是,此表使用「用戶」表中的外鍵,所以我想要CSV文件包含來自「用戶」表的這個唯一標識符。

有沒有解決方案?

回答

2

對不起,反應遲緩。 最近,我爲gem wiki添加了一個新示例,它與您的問題非常相似。 它可以使用自定義的before_batch_importmaster分支

下一個例子來解決演示瞭如何動態地執行插入查詢之前解決從作者的姓名和更改CSV值AUTHOR_ID值。

ActiveAdmin.register Post do 
      active_admin_import validate: true, 
       headers_rewrites: { :'Author name' => :author_id }, 
       before_batch_import: ->(importer) { 
       authors_names = importer.values_at(:author_id) 
       # replacing author name with author id 
       authors = Author.where(name: authors_names).pluck(:name, :id) 
       options = Hash[*authors.flatten] # #{"Jane" => 2, "John" => 1} 
       importer.batch_replace(:author_id, options) #replacing "Jane" with 1, etc 
       } 
     end 
+0

太好了!就是這樣。 – 2015-01-23 21:26:48