0

I wrote many simple rake files to import txt files to my mysql. Everything work perfectly except one model. I have no errors so I don't know what happening.

The rake imports the first line only. Everything else don't!

The txt is on UTF8 encoding, by the way.

Could be the counter_cache of recipe's associations?

RAILS 3.1

Model:

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 file:

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
maiconsanson
  • 243
  • 3
  • 16
  • Why are you manually setting the id field? – Richard Brown Mar 05 '13 at 22:29
  • Cause the old data id starts with 53. But I could adjust this change in auto increment on DB too. No problems. – maiconsanson Mar 05 '13 at 22:34
  • I'd do that... and add a `puts d.inspect` in the loop just to make sure it's actually looping through all the records... – Richard Brown Mar 05 '13 at 22:37
  • Thanks @RichardBrown. I did that but the loop puts FINISHED right after first record...`Importing Recipe... # =========== > FINISHED!` – maiconsanson Mar 05 '13 at 22:49
  • 1
    Ok then, it points to there being an issue in the format of the file... – Richard Brown Mar 05 '13 at 22:52
  • Are you sure? I created and wrote a new file with two records by hand for tests. I'm using more five import rake with same format and only this one has a problem. Could be a model problem with their associations? – maiconsanson Mar 05 '13 at 22:58
  • If it's not throwing an error I find it hard to believe. To make 100% sure just comment out the `Recipe.new` line and replace it with a `puts line` until it outputs ALL the lines. Then work backwards from there. – Richard Brown Mar 05 '13 at 23:01
  • @RichardBrown you are 100% right! File format problem! I've created new files with same name so I have the same error. But now I changed the file name e voilá: It works! – maiconsanson Mar 05 '13 at 23:07

0 Answers0