简体   繁体   English

如何使用 rails_admin_import gem 导入关联的 model?

[英]How to import model with associations using rails_admin_import gem?

I'm trying to import my database, which have Project and Technology models (project has many technologies).我正在尝试导入我的数据库,它有ProjectTechnology模型(项目有很多技术)。 Each project should have at least 1 technology.每个项目应该至少有 1 项技术。 I use rails_admin_import gem for this task.我使用 rails_admin_import gem 来完成这项任务。 As far as I understand, during project import gem is supposed to find associations of the model (technologies in my case) and connect them to model if they exist.据我了解,在项目导入期间,gem 应该找到 model 的关联(在我的案例中是技术)并将它们连接到 model(如果存在)。

Everything is fine when I import technologies, but as soon as I'm importing projects, I receive this error for each of the projects:当我导入技术时一切都很好,但是当我导入项目时,每个项目都会收到此错误:

Failed to create Project_1: Technologies minimum allowed: 1

I tried to import projects before importing technologies and after that, added to both of them mapping key, but nothing changed:我尝试在导入技术之前和之后导入项目,将映射键添加到它们中,但没有任何改变:

config.model 'Project' do
  import do
    mapping_key :name
    include_all_fields
  end
end
config.model 'Technology' do
  import do
    mapping_key :name
    include_all_fields
  end
end

Maybe anyone can help me and say what am I doing wrong?也许任何人都可以帮助我说我做错了什么?

UPD: The problem is that I exported data using default rails_admin export, which saves data in csv like that: UPD:问题是我使用默认的 rails_admin export 导出数据,它像这样将数据保存在 csv 中:

在此处输入图像描述

But rails_admin_import expects data in this format:但是 rails_admin_import 需要这种格式的数据:

在此处输入图像描述

So I need to change my question.所以我需要改变我的问题。 How should I configure rails_admin so that it would export data in a format accepted by rails_admin_import?我应该如何配置 rails_admin 以便它以 rails_admin_import 接受的格式导出数据?

I found a way to do this, but probably not the best one.我找到了一种方法来做到这一点,但可能不是最好的方法。

I used before_import_associations method of rails_admin_import to create needed technologies key manually.我使用before_import_associations的 before_import_associations 方法手动创建所需的technologies密钥。

def before_import_associations(record)
  record[:technologies] = record[:name_technologies].split(',')
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM