Rod*_*ves 2 ruby csv ruby-on-rails activeadmin
目前我有一个 ActiveAdmin 页面,它从 CSV 文件获取数据以填充数据库表(用户)。该表有一个唯一标识符,该标识符不是关系中使用的ID(供用户查看的用户友好代码)。该页面通过“active_admin_import”执行此操作
现在,我想要同样的东西填充另一个表(user_paths),问题是,该表使用“users”表中的外键,所以我希望 CSV 文件包含“users”表中的这个唯一标识符。
有什么解决办法吗?
抱歉迟了回应。最近我向 gem wiki添加了与您的问题非常相似的新示例。可以通过使用分支的自定义before_batch_import钩子来解决master
下一个示例演示如何在执行插入查询之前从作者姓名解析author_id值并动态更改csv值。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1973 次 |
| 最近记录: |