Rake not generating complete model rows

22 views Asked by At

I am trying to import some CSV rows into a simple Add model with only two columns: 'title' and 'description' but somehow the code is not generating a complete Add row with Add.new(), this is the model:

class Add < ApplicationRecord
  scope :descending, -> { order("created_at DESC") }
  scope :ascending, -> { order("created_at ASC") }

  after_create_commit { broadcast_prepend_to "adds" }
  after_update_commit { broadcast_replace_to "adds" }
  after_destroy_commit { broadcast_remove_to "adds" }

  validates_presence_of :title, :description
  validates :title, length: { minimum: 3, maximum: 100 }
  validates :description, length: { minimum: 3, maximum: 2000 }

  has_rich_text :description
end

This is the Rake task:

namespace :import do
  desc "Import adds from CSV"
  task :anuncios, [:file_path] => :environment do |t, args|
    require "csv"
    puts "Importing Adds"
    model = Add
    
    records = []
    CSV.foreach(args.file_path, headers: true) do |row|
      pp row['title']
      pp row['description']
      # next if records.any? {|rec| rec['title'] == row['title']} || model.find_by(title: row['title']).present?
      records << model.new(title: row['title'], description: row['description'])

    end
    pp records
    # model.import! records
    puts "#{records.size} Adds imported successfully"
  end

end

I have commented the lines to import into the database for simplicity and debugging. This is the CSV file:

title,description
uno,Cualquier cosa para el primer registro
dos,Cualquier cosa para el segundo registro
tres,Cualquier cosa para el tercer registro
cuatro,Cualquier cosa para el cuarto registro
cinco,Cualquier cosa para el quinto registro
seis,Cualquier cosa para el sexto registro
siete,Cualquier cosa para el séptimo registro
ocho,Cualquier cosa para el octavo registro
nueve,Cualquier cosa para el noveno registro
diez,Cualquier cosa para el décimo registro

This is the output:

 $ rake import:anuncios['tmp/storage/adds.csv']
Importing Adds
"uno"
"Cualquier cosa para el primer registro"
"dos"
"Cualquier cosa para el segundo registro"
"tres"
"Cualquier cosa para el tercer registro"
"cuatro"
"Cualquier cosa para el cuarto registro"
"cinco"
"Cualquier cosa para el quinto registro"
"seis"
"Cualquier cosa para el sexto registro"
"siete"
"Cualquier cosa para el séptimo registro"
"ocho"
"Cualquier cosa para el octavo registro"
"nueve"
"Cualquier cosa para el noveno registro"
"diez"
"Cualquier cosa para el décimo registro"
[#<Add:0x00007ff57e695548 id: nil, title: "uno", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57f6f6ba8 id: nil, title: "dos", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57f75b300 id: nil, title: "tres", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57f78cd38 id: nil, title: "cuatro", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57f7c4aa8 id: nil, title: "cinco", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57f7d8878 id: nil, title: "seis", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57f6d3bd0 id: nil, title: "siete", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57fb56ba8 id: nil, title: "ocho", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57fb5e3f8 id: nil, title: "nueve", description: nil, created_at: nil, updated_at: nil>,
 #<Add:0x00007ff57fba31d8 id: nil, title: "diez", description: nil, created_at: nil, updated_at: nil>]
10 Adds imported successfully

It's driving me crazy not understanding why I get description: nil If printing it gives me the right content. Please help me

this is the schema:

create_table "adds", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

So it seems to be according to whats expected but then it is not creating the model's content and NO ERROR MESSAGE ...

0

There are 0 answers