Search code examples
rhomobilerhodes

How can i seed data from a csv file in Rhodes


I have seen in Rhomobile we can seed the local db from local files. The file need to be text file, and the format something like this,

client_id|last_sync_success
67320d31-e42e-4156-af91-5d9bd7175b08|

But can i have some way to seed data from a csv file ?


Solution

  • You can use something like below,

    # For seeding question to question table from question.csv file
    if MyModel.find(:all).empty?
      file_name = File.join(Rho::RhoApplication.get_app_path('public')+'sample.csv')
      file = File.new(file_name, "r")
      while (line = file.gets)
        col = (line.gsub("\n", "")).split(";")
        MyModel.create( {"id" => col[0].gsub('"',''), "text" => col[1].gsub('"','')} )
      end
    end
    

    Hope this helps you.