2016-11-09 30 views
1

我正在構建一個將應用程序與許多不同類別關聯的應用程序。例如,我有一個帖子,需要能夠通過csv導入通過rake任務將其分配到體育,新聞和科學類別。Rails csv與協會的導入

我的問題是如何將多個category_ids的數組導入到Post模型中?我有它的工作,我可以手動創建一個新的職位,並指定多個類別的職位,但我很困惑,如何通過CSV完成。我需要幫助找出實現這一目標的最佳方法。

這是我到目前爲止有:

模式

create_table "posts", force: :cascade do |t| 
    t.string "name" 
    t.text  "description" 
    end 

    create_table "styles", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "post_id" 
    t.integer "category_id" 
    end 

    create_table "categories", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 

Post.rb

class Post < ApplicationRecord 
    has_many :styles 
    has_many :categories, through: :styles 
end 

Category.rb

class Category < ApplicationRecord 
    has_many :styles 
    has_many :posts, through: :styles 
end 

Style.rb

class Style < ApplicationRecord 
    belongs_to :post 
    belongs_to :category 
end 

Rake任務

require 'csv' 
require 'open-uri' 
namespace :post_import do 
    desc "Import posts daily" 
    task posts: :environment do 
    filename = File.join Rails.root, "posts.csv" 
    counter = 0 

    CSV.foreach(filename) do |row| 
     name, category, description = row 
     post = Post.create(name: name, category_ids: category, description: description) 
     counter += 1 if post.persisted? 
    end 

    puts "Imported #{counter} [posts]" 
    end 
end 

回答

0

我能夠從克里斯的幫助,在gorails.com來解決這個問題。這是我的(粗糙的)工作解決方案。希望這可以幫助其他人解決這個問題!

require 'csv' 
require 'open-uri' 
namespace :post_import do 
    desc "Import posts daily" 
    task posts: :environment do 
    filename = File.join Rails.root, "posts.csv" 
    counter = 0 

    CSV.foreach(filename) do |row| 
     name, category_ids, description = row 
     post = Post.new(name: name, description: description) if post == nil 
     post.save 

     #separate string of category ids into array 
     a_categories = category_ids.split(",") 

     a_categories.each do |category_id| 
     post.styles.where(category_id: category_id).first_or_create 
     end 

     counter += 1 if post.persisted? 
    end 

    puts "Imported #{counter} [posts] 
    end 
end 
0
require 'csv' 
require 'open-uri' 
namespace :post_import do 
    desc "Import posts daily" 
    task posts: :environment do 
    filename = File.join Rails.root, "posts.csv" 
    counter = 0 

    CSV.foreach(filename) do |row| 
     name, category, description = row 

     #you can user find or create, but for clarity I split it 
     post = Post.where(name: name).first 
     post = Post.new(name: name, description: description) if post == nil 

     #now we can simply add the style which joins to the category 
     post.styles.build(category_id: category) 
     post.save 
     counter += 1 if post.persisted? 
    end 

    puts "Imported #{counter} [posts]" 
    end 
end 
+1

請解釋爲什麼這是一個合適的答案。而不是拋出代碼幫助教育。 –

+0

如何將帖子分配給多個類別?例如,在類別行的csv中,我有「1,2,3」代表3個不同類別的ID,但只記錄第一個ID。另外,爲什麼你使用.new vs .create?謝謝您的幫助 – b1akely