2016-11-09 15 views
1

投稿をさまざまなカテゴリに関連付けるレールアプリケーションを構築しています。たとえば、私は投稿を持っており、rakeタスクを介してcsv importを通してスポーツ、ニュース、科学のカテゴリに割り当てることができる必要があります。Rails csv import with associations

私の質問は、複数のcategory_idsの配列を投稿モデルにインポートする方法です。私は手動で新しい投稿を作成し、投稿に複数のカテゴリを割り当てることができるところで働いていますが、私は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には、3つの別々のカテゴリーのIDを表す「1,2,3」がありますが、最初のIDだけが記録されています。また、なぜ新しい.vs .createを使うのですか?助けてくれてありがとう – b1akely