导入CSV时,如何处理与关联对应的行中的数据? [英] When importing a CSV, how do I handle data in a row that corresponds to an association?

查看:354
本文介绍了导入CSV时,如何处理与关联对应的行中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循导入CSV轨道,这是直截了当的。



问题是它只处理一个csv文件,其中只包含1个文件中1个模型中的信息。



说,我有一个CSV文件,我试图导入到我的列表模型。在每一行/列表上,它有一个名为 Building 的列,其中的值实际上是该列表的建筑属性的名称(即 @ listing.building



如何处理导入中的这些情况?

>这是Ryan在那个Railscast里的衣橱,它在他的案例中的 Product 模型:

  def self.import(file)
CSV.foreach(file.path,headers:true)do | row |
product = find_by_id(row [id])|| new
product.attributes = row.to_hash.slice(* accessible_attributes)
product.save!
end
end

所有这一切都发生在他正在检查如果产品存在,并且如果它然后更新属性。如果没有,请创建一个新的。



不太确定如何处理这种情况下的关联...特别是假设需要发生的是



回到我的 building.name

/ code>如果没有 Building.find_by_name(name),那么它应该创建一个新的建筑物记录。





<$>

p $ p> def self.import(file)
CSV.foreach(file.path,headers:true)do | row |
product = find_by_id(row [id])|| new
product.attributes = row.to_hash.slice(* accessible_attributes)
product.save!

building = product.buildings.find_by_name(row ['building_name'])
building || = product.buildings.build
building.attributes = row.to_hash.slice * build_accessible_attributes)
building.save!
end
end

更新:使用new rails 3方法更新答案



  def self.import(file)
CSV.foreach(file.path,headers:true)do | row |
product = where(id:row [id])
.first_or_create!(row.to_hash.slice(* accessible_attributes))

product.buildings.where :row ['building_name'])
.first_or_create!(row.to_hash.slice(* building_accessible_attributes))
end
end


I am following the Import CSV Railscast and it is straight forward.

The issue is that it deals with just a csv file containing only information in 1 model in 1 file.

Say, I have a CSV file that I am trying to import to my Listing model. On each row/listing it has a column called Building where the value is actually the name of the building attribute of that listing (i.e. @listing.building.name).

How do I handle those cases in the import?

This is the closet that Ryan gets in that Railscast, which is on the Product model in his case:

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.slice(*accessible_attributes)
    product.save!
  end
end

All that is happening is that he is checking to see if the product exists and if it does then update the attributes. If it doesn't, then create a new one.

Not quite sure how to handle the associations in this case...especially given that what needs to happen is in the event that an associated record doesn't exist, it needs to be created during this process.

So going back to my building.name example earlier, if there is no Building.find_by_name(name), then it should create a new building record.

Thoughts?

解决方案

try this

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.slice(*accessible_attributes)
    product.save!

    building = product.buildings.find_by_name(row['building_name'])
    building ||= product.buildings.build
    building.attributes = row.to_hash.slice(*build_accessible_attributes)
    building.save!
  end
end

UPDATE: updated answers using new rails 3 methods

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    product = where(id: row["id"])
      .first_or_create!(row.to_hash.slice(*accessible_attributes))

    product.buildings.where(name: row['building_name'])
      .first_or_create!(row.to_hash.slice(*building_accessible_attributes))
  end
end

这篇关于导入CSV时,如何处理与关联对应的行中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆