1

I would like to build an air cargo app. I want each cargo to be attached to one destination airport.

I found this JSON file. Sample:

"iata": "FOB",
"lon": "-123.79444",
"iso": "US",
"status": 1,
"name": "Fort Bragg Airport",
"continent": "NA",
"type": "airport",
"lat": "39.474445",
"size": "small"
  • Where should I put the JSON file in a rails 4 app?
  • How can I autocomplete airports both in "iata" and "name" field?
  • Given the size(~1.7mb) of the file which method other than "filter method" should I use, preferably in reactjs?
The questioner
  • 724
  • 10
  • 21

1 Answers1

0

First, I would create a rake task or something similar to run the JSON to the dedicated database table (for example model called Airport). Here is some examples for running JSON to the database. This way you can also update the airport data when it has changed and the searching becomes much easier since you can use ActiveRecord for it.

Second, I would probably place the JSON file under config/ folder.

And finally about autocomplete. Since you haven't too explicitly told what you wish from the autocomplete, you could for example use jQuery-Autocomplete with what you could write something like this

$('#autocomplete').autocomplete({
  lookup: function (query, done) {
    // Do ajax call with the query
    $.ajax("www.your-api.com/search?query=" + query).done(function (data) {
      done({result: data});
    });
  },
  onSelect: function (suggestion) {
    alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
  }
});

It is quite impossible to give better instructions on how to do the autocomplete, but this way you can anyway autocomplete by 2 different fields.

Community
  • 1
  • 1
Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
  • Thank you for your answer. Given there are over 6000 airports, is fetching a record from the DB **faster** than searching a JSON object from a file? My plan: create a `Cargo` table, create an `iata` field there for referencing an airport. I treat `iata` as a category such that every time I search `Cargo.where("iata = ?", params[:from_json])` all the cargo shipments will show up. That means I just need "iata" and "name", in this scenario, would you still suggest migrating JSON data to the database? – The questioner Aug 23 '15 at 18:13
  • To further explain my plan: 1. an array of relevant airport is listed by searching keywords; 2. when clicked, a JSON object turned and airport `name` stays in the input field whilst the `iata` is used to search for cargo shipments. – The questioner Aug 23 '15 at 18:21
  • @Thequestioner: I would claim that the database is way faster especially when correct indexes (probably at least _IATA_ and _name_ since you're searching based on them constantly) are set for your table. With that db approach you would write something like `Cargo.joins(:airports).where('airports.iata = ? or airports.name = ?', params[:iata], params[:name])` to fetch the suggestions for autocomplete. To your second comment, point 1 should be already discussed. Point 2 would also be quite easy to implement this way as all data is available in frontend for those purposes. – Roope Hakulinen Aug 24 '15 at 07:34
  • Thanks, I would use the database approach then. – The questioner Aug 24 '15 at 08:13