I have a strange issue with to_json method in my Rails 3 app. (well at least I think it is to do with to_json)
In my controller, I am getting the list of all the libraries stored in the DB
@libraries = Library.where( "latitude IS NOT NULL AND longitude IS NOT NULL" )
And then I create a json file that contains the library information above.
my_file = File.new("public/javascripts/libraries.json", "w")
my_file.write "var libs = {'libraries' : "
my_file.write @libraries.to_json( :only => [ :id, :name, :address, :latitude, :longitude ])
my_file.write "};"
Then in my view, I display each library object on Google Map. In the view file, I am reading the json file by loading the libraries.json file as a javascript file.
Now the problem is that the library objects are displayed on Google map SOMETIMES, but not all the time, and through Firebug, I was able to determine that sometimes the "libs" variable, that is contained in the JSON file is "undefined".
This makes me think that the file has not been completely being written, or the data in the file has not been completely been loaded. But I am not too sure what it is.
Does anyone have an idea what could be causing this?