No description, website, or topics provided.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
db/migrate Fixing migrations. Not sure what I was thinking before :/ Apr 19, 2016
lib 2.1.2 Nov 15, 2016
spec make import spec pass Apr 19, 2016
.gitignore Stabalize Tests May 2, 2015
.travis.yml Remove 1.9.3 from travis, support ended for that in feb 2015 Apr 19, 2016 2.1.0 Apr 19, 2016
Gemfile First commit Dec 23, 2013
LICENSE.txt First commit Dec 23, 2013 Update README Apr 19, 2016
Rakefile First commit Dec 23, 2013
usda-nutrient-database.gemspec Add composite primary keys Apr 20, 2016

Stories in Ready Code Climate Build Status Coverage Status Dependency Status Gem Version Gitter chat

USDA Nutrient Database

The USDA nutrition database is a great source of nutrition information. However, the data formatting options (plain text ASCII files or MS-Access) leave a little to be desired.

This is a simple gem to import the database into your ruby application.

Using the USDA Nutrient Database REST API instead

Even after importing this data, keeping it up to date and adding/maintaining additional logic such as full text search, weight conversion, etc is a pain. As an alternative I have put together a small RESTful web service to do this for you. You can find the docs at


Add this line to your application's Gemfile:

gem 'usda-nutrient-database'

And then execute:

$ bundle

Or install it yourself as:

$ gem install usda-nutrient-database

If you're using rails then copy the migrations across:

rake usda_nutrient_database_engine:install:migrations


UsdaNutrientDatabase.configure do |config|
  config.batch_size = 20000 # import batch size, if using activerecord-import
  config.perform_logging = true # default false
  config.logger = Rails.logger # default
  config.usda_version = 'sr25' # default sr28


Importing with UPSERT (takes around 2 minutes)

If you are running MySQL => 5.6 or PostgreSQL => 9.5 then you're in luck, you can use UPSERT (insert or update) to speed up imports x30. To do this you're going to need to install activerecord-import

require 'activerecord-import/base'

# You may want to disable logging during this process to avoid dumping huge SQL
# strings in to your logs
ActiveRecord::Base.logger ='/dev/null')

Now run the rake task which will import everything in around 2 minutes.

rake usda:import

Importing without UPSERT (takes 60+ minutes)

Import the latest data with the import task:

rake usda:import

This is going to take a while. 60+ minutes on my 2.66 GHz i7 macbook pro. You ca also import individual tables using the other import tasks. To see the available tasks:

rake -T usda


Use the models to query and profit:



If you find a bug then add it to the issues here on github. Ideally with a pull request to fix it, or at least with a failing test. When you report a bug, number 1 rule is: don't be a dick.

Any feature requests/ideas can also go in the issues list.


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request


Many thanks to the following contributers:


This gem follows Semantic Versioning