Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4560548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:56:44+00:00 2026-05-21T17:56:44+00:00

I have a strange issue with to_json method in my Rails 3 app. (well

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:56:45+00:00Added an answer on May 21, 2026 at 5:56 pm

    Yes, it is probably caused by the fact that you write it to a file instead of just including it in a tag. Because what probably happens (sometimes) is that the server gets the request, writes the json file, then renders a view and sending that view to the client. The client is then told to load the libraries.json file resulting in another request to the server to retrieve that file. But what if that file in that exact moment is being rewritten because another client has sent a request at the same time? It will fail.

    There are a few other ways you could go about this. First and probably the quickest to get up and running is to include this in your controller and view instead of writing to a file:

    #controller
    @libraries = Library.where( "latitude IS NOT NULL AND longitude IS NOT NULL" ).to_json( :only => [ :id, :name, :address, :latitude, :longitude ])
    
    #view
    <script type="text/javascript">
    var libs = {'libraries': <%= @libraries.html_safe %>}
    </script>
    

    But a better solution would probably be to schedule some job or task to load the library from the database at schedule intervals and write to a json file perhaps once a day because I guess it is not really necessary to load these from the database all the time unless they change from one minute to the next.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this strange issue with my web app. You see, I'm using jQuery
I have a strange issue (at least for me :)) with the MySQL's locking
Hi guys I have a strange issue. I am trying to get list of
I have a strange issue in my GWT app. My application is working fine
I have some strange issue using jQuery Validation plugin. Firs of all here is
I have a really strange issue in my project. I have a facebook app
I have a very strange issue with Daylight Savings Time (DST) in my app.
I am trying to debug a strange issue with users that have LogMeIn installed.
I have a strange issue: I am using SPContext.Current.Web in a .aspx page, but
I have a strange issue that has arisen recently: Whenever I enter text, even

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.