Skip to content

Instantly share code, notes, and snippets.

@madetech-com
Created June 8, 2017 09:56
Show Gist options
  • Save madetech-com/38c1110d53acfe51d528bb6c86f68528 to your computer and use it in GitHub Desktop.
Save madetech-com/38c1110d53acfe51d528bb6c86f68528 to your computer and use it in GitHub Desktop.
Dumb Things With Postgres
class Article < ActiveRecord::Base
# all the other logic
def associated_articles
Article.where.not(id: id).where('tags && ARRAY[?]', tags)
end
end
class ChangeXIdColumn < ActiveRecord::Migration
def up
remove_index :products, :product_category_id
change_column :products, :product_category_id, 'integer[] USING ARRAY[product_category_id]::INTEGER[]', array: true, null: false, default: []
rename_column :products, :product_category_id, :product_category_ids
add_index :products, :product_category_ids, using: 'gin'
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
change_column :products, :product_category_id, 'integer[] USING ARRAY[product_category_id]::INTEGER[]', array: true, null: false, default: []
add_index :products, :product_category_ids, using: 'gin'
def product_categories
ProductCategories.where(id: product_category_ids)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment