Manish Nautiyal
2012-Mar-09 07:28 UTC
CSV problem with migration but working fine in controller
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 def self.down Language.destroy_all end end -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2012-Mar-09 09:09 UTC
Re: CSV problem with migration but working fine in controller
On 9 March 2012 07:28, Manish Nautiyal <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > endPut 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.