Skip to content

Instantly share code, notes, and snippets.

@ubergeekzone
Created March 8, 2016 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ubergeekzone/550ba8f4a80f104e7453 to your computer and use it in GitHub Desktop.
Save ubergeekzone/550ba8f4a80f104e7453 to your computer and use it in GitHub Desktop.
A Sample Ruby ActiveRecord DB Migrate File
create_table :orders do |t|
t.string 'name'
end
add_index :orders, :tags, using: 'gin'
add_index :orders, :ratings, using: 'gin'
# app/models/order.rb
class Order < ApplicationRecord
end
# Usage
Order.create name: "INV-100012",
tags: ["books", "misc"],
ratings: [1, 3]
Order.where("'books' = ANY (tags)") #orders tag by single
Order.where("tags @> ARRAY[?]::varchar[]", ["books", "misc"]) #orders tags by array
Order.where("array_length(ratings, 1) >= 3") #orders with 3 or more ratings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment