Created
June 8, 2017 09:56
-
-
Save madetech-com/38c1110d53acfe51d528bb6c86f68528 to your computer and use it in GitHub Desktop.
Dumb Things With Postgres
This file contains hidden or 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
class Article < ActiveRecord::Base | |
# all the other logic | |
def associated_articles | |
Article.where.not(id: id).where('tags && ARRAY[?]', tags) | |
end | |
end |
This file contains hidden or 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
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 |
This file contains hidden or 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
change_column :products, :product_category_id, 'integer[] USING ARRAY[product_category_id]::INTEGER[]', array: true, null: false, default: [] |
This file contains hidden or 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
add_index :products, :product_category_ids, using: 'gin' |
This file contains hidden or 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
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