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@googlegroups.com.
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.

Search Discussions

  • Colin Law at Mar 9, 2012 at 9:10 am

    On 9 March 2012 07:28, Manish Nautiyal 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 rubyonrails-talk@googlegroups.com.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 9, '12 at 7:28a
activeMar 9, '12 at 9:10a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Manish Nautiyal: 1 post Colin Law: 1 post

People

Translate

site design / logo © 2022 Grokbase