First create a text file (let’s say data.txt) under the Public folder of the App where to write the sample data in JSON format..
Now write the sample data in the json format in the data.txt as follows.
[ {"bookName": "Learning MySQL", "bookAuthorName": "Seyed M.M. (Saied) Tahaghoghi"}, {"bookName": "PHP Cookbook: Solutions and Examples for PHP Programmers", "bookAuthorName": "Adam Trachtenberg"}, {"bookName": "Understanding MySQL Internals", "bookAuthorName": "Sasha Pachev" } ]
Now write the following code in the application.rb file(present in the base app folder) under the ‘initialize’ method
bookInfo = Book.find(:all) //here Book is the Model if bookInfo.empty? fileName = File.join(Rho::RhoApplication::get_base_app_path(), '/public/data.txt') fileContent = File.read(fileName) jsonContent = Rho::JSON.parse(fileContent) //parses the json data jsonContent.each { |json| Book.create("bookName" => json['bookName'], "bookAuthorName" => json['bookAuthorName'] ) }
else
#the data present in the bookInfo will be loaded
end