Created
March 8, 2016 14:51
-
-
Save ubergeekzone/550ba8f4a80f104e7453 to your computer and use it in GitHub Desktop.
A Sample Ruby ActiveRecord DB Migrate File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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