0

txtファイルをmysqlにインポートするために多くの単純なrakeファイルを作成しました。1つのモデルを除いて、すべてが完全に機能します。エラーがないので、何が起こっているのかわかりません。

レーキは最初の行のみをインポートします。他のすべてはしません!

ちなみに、txtはUTF8エンコーディングです。

レシピの関連付けのcounter_cacheである可能性がありますか?

レール3.1

モデル:

class Recipe < ActiveRecord::Base
    belongs_to :chef, :counter_cache => true
    belongs_to :category, :counter_cache => true
    belongs_to :cuisine, :counter_cache => true
    belongs_to :festivity, :counter_cache => true
    belongs_to :daily, :counter_cache => true

    after_update :update_counter

    # Setup accessible (or protected) attributes for your model
    attr_accessible :name,
                    :slug,
                    :description,
                    :ingredients,
                    :steps,
                    :...
                    :status_id,
                    :created_at,
                    :updated_at

    STATUS = { 'Não publicada' => 0, 'Publicada' => 1, 'Invisível' => 4 }
    def status
        STATUS.invert[status_id]
    end

    private

    def update_counter
        if category_id_changed?
            Category.increment_counter(:recipes_count, category_id)
            Category.decrement_counter(:recipes_count, category_id_was)
        end
        if cuisine_id_changed?
            Cuisine.increment_counter(:recipes_count, cuisine_id)
            Cuisine.decrement_counter(:recipes_count, cuisine_id_was)
        end
        if festivity_id_changed?
            Festivity.increment_counter(:recipes_count, festivity_id)
            Festivity.decrement_counter(:recipes_count, festivity_id_was)
        end
        if daily_id_changed?
            Daily.increment_counter(:recipes_count, daily_id)
            Daily.decrement_counter(:recipes_count, daily_id_was)
        end
    end
end

RAKEファイル:

namespace :db do
    desc "import data from files to database"
    task :import_recipe => :environment do
        puts "Importing Recipe..."
        # recipe.txt
        File.open("lib/tasks/data/recipe.txt", "r").each do |line|
            id, name, slug, description, ingredients, steps, other_tips, cook_time, recipe_yield, diet, light, lactose_intolerance, vegetarian, microwave, video_url, chef_id, category_id, cuisine_id, festivity_id, daily_id, likes_count, rating, ratings_count, views_count, comments_count, status_id, created_at, updated_at = line.strip.split("\t")
            d = Recipe.new(
                :id => id,
                :name => name,
                :slug => slug,
                :description => description,
                :ingredients => ingredients,
                :steps => steps,
                :...
                :status_id => status_id,
                :created_at => created_at,
                :updated_at => updated_at
                )
                d.save!
            end
        puts "=========== > FINISHED!"
    end
end
4

0 に答える 0