On 9 March 2012 07:28, Manish Nautiyal <[email protected]> wrote: > I'm getting problem with csv. When I use below code in my Controller > method then it works but when I put this in my migration then this don't > work. Why so > > ============================================================ > In My Controller's index method. It works. > > > require 'csv' > > CSV.foreach("#{Rails.root}/db/adwords_location_data/languages.csv") > do |row| > obj_language = Language.new > obj_language.name = row[1] > obj_language.adwords_id = row[0] > obj_language.save > end > > =========================================================== > But When I write this in my migration this doesn't work. No error come > but data didn't inserted in the tables. Below is the migration code. > > require "csv" > > class LoadLanguageData < ActiveRecord::Migration > def self.up > CSV.foreach("#{Rails.root}/db/adwords_location_data/languages.csv") do > |row| > obj_language = Language.new > obj_language.name = row[1] > obj_language.adwords_id = row[0] > obj_language.save > end > end
Put in some debug to work out what is going wrong. Is it executing self.up? Is it finding the file? Does it find any rows? Does the save fail (it might be worth testing this first by checking the return from save, perhaps validations are failing). Have a look at the Rails Guide on Debugging for debugging techniques. Knowing how to debug is one of the most important skills, in five minutes debugging you can save the time spent asking questions and waiting hours for help. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

