RuboCop and the 80 character line length limit is absurd

Having a line length limit is absurd. Just change your wrap setting in vim! IMO this gives you best of both worlds you can have everything on the screen when you need it or off when you don’t. Also, it’s easier to read indentation and syntax on one liner code snippets. When the code flows off the screen it can hide a lot of grit and make it easier to get a general lay of the land (ie. control logic) and then turn wrap on and hack.

Every time you adjust the select query you have to fiddle with the line length because OMG, it might overflow 80 and break your mac.

I myself think any line length limit is absurd, just use common sense. If it is clearer to break, then break it, otherwise don’t. But breaking it for the sake of an arbitrary length limit?

You’re inevitably going to use a line limit (You might even agree with it), so put this in your .vimrc, it highlights the 100th character. (Yeah I use a 100 character line length limit). I think Github displays 120 characters so that might actually make the most sense.

autocmd Filetype ruby highlight ColorColumn ctermbg=red
autocmd Filetype ruby call matchadd('ColorColumn', '\%101v', 120)

Elastic search order by date and indexing with Ruby-on-Rails

The elastic search date field type seems like a lot of work. You have to manually map each date field parameter. All I really wanted to do was order by date, and this doesn’t necessarily rule out more complicated date range filtering. You just have to convert everything to Epoch millisecond time. Sometimes it just seems like the most standard format.

Map the field type in your elastic search index as a float. Changing a field type requires recreating the index.

mappings dynamic: 'false' do
 indexes :created_at, type: 'float'
end

In your method that converts the model to json for indexing, convert the date to milliseconds.

def as_indexed_json(options={})
  model_attrs = {
    :created_at => self.created_at.to_f
  }
end

Now you can easily order by asc and desc.

USDA Nutrient Data SR23 POSTGRES SQL dump

I found the mysql dump online but these days I prefer Postgres. For others who are in the same boat as me, I thought I would save you the troubles! Enjoy.

USDA Nutrient Data (SR23) Postgres Dump

I used py-mysql2pgsql and renamed all the tables to lower case.

I plan to hook this data into elastic search so that I can search on it with a rails api and return JSON. Maybe I’ll open source the elastic search rails API I’m going to build.

UPDATE:

I noticed that this data set is missing the food categories, you can seed these with this:

# db/seeds.rb

groups = [
  ["0100", "Dairy and Egg Products"],
  ["0200", "Spices and Herbs"],
  ["0300", "Baby Foods"],
  ["0400", "Fats and Oils"],
  ["0500", "Poultry Products"],
  ["0600", "Soups, Sauces, and Gravies"],
  ["0700", "Sausages and Luncheon Meats"],
  ["0800", "Breakfast Cereals"],
  ["0900", "Fruits and Fruit Juices"],
  ["1000", "Pork Products"],
  ["1100", "Vegetables and Vegetable Products"],
  ["1200", "Nut and Seed Products"],
  ["1300", "Beef Products"],
  ["1400", "Beverages"],
  ["1500", "Finfish and Shellfish Products"],
  ["1600", "Legumes and Legume Products"],
  ["1700", "Lamb, Veal, and Game Products"],
  ["1800", "Baked Products"],
  ["1900", "Sweets"],
  ["2000", "Cereal Grains and Pasta"],
  ["2100", "Fast Foods"],
  ["2200", "Meals, Entrees, and Side Dishes"],
  ["2500", "Snacks"],
  ["3500", "American Indian/Alaska Native Foods"],
  ["3600", "Restaurant Foods"]
]

groups.each do |g|
  FoodGroup.first_or_create(FdGrp_Cd: g[0], FdGrp_Desc: g[1])
end

Additionally, if you’d like some rails models for your api, this might be a good start:

# app/models/food.rb
class Food < ActiveRecord::Base
  self.table_name = "food_des"
  self.primary_key = "NDB_No"

  has_many :measures, :foreign_key => "NDB_No"
  has_one :food_group, primary_key: "FdGrp_Cd", :foreign_key =>  "FdGrp_Cd"
end

# app/models/food_group.rb
class FoodGroup < ActiveRecord::Base
  self.table_name = "fd_group"
end

# app/models/measure.rb
class Measure < ActiveRecord::Base
  self.table_name = "weight"

  belongs_to :food, primary_key: "NDB_No", :foreign_key =>  "NDB_No"
end

Rails 4.2 and Elastic Search – Querying and Filtering Across Multiple Nested Models

At work recently I have been working on a new filtering and search page for ourselves. The requirements were searching and filtering said search results. Things get complicated and performance tends to suffer when dealing with complicated and far reaching relationships. You end joining every table in the database and even then you are only filtering and not actually searching. Sometimes denormalized data just makes sense. It’s still a WIP but here it as anyway, hope this snippet helps you get rolling with your project.

def self.search(query="", options={})

   params = {
      query: {
        filtered: {
          query: {
            multi_match: {
              query: query,
              fields: ['title^10', 'overview']
            },
            match_all: {}
          },
          filter: {
            bool: {
              must: [
                {
                  nested: {
                    path: 'regions',
                    filter: {
                      bool: {
                        must: [
                          {
                            terms: { 'regions.id' => options[:regions] }
                          }
                        ]
                      }
                    }
                  }
                },
                {
                  nested: {
                    path: 'genres',
                    filter: {
                      bool: {
                        must: [
                          {
                            terms: { 'genres.id' => options[:genres] }
                          }
                        ]
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      },
      sort: [
        {
          options[:col].try(:downcase) => {
            order: options[:direction].try(:downcase) #, ignore_unmapped: true
          }
        }
      ]
    }

    params[:query][:filtered][:query].delete(:match_all) if query.present?
    params[:query][:filtered][:query].delete(:multi_match) if query.blank?
    params.delete(:sort) if options[:col].blank? || options[:direction].blank?

    __elasticsearch__.search(params).page(options[:page]).per(options[:limit])
  end