Skip to content

Instantly share code, notes, and snippets.

View Froks15's full-sized avatar
🎯
Focusing

Movses Froks15

🎯
Focusing
View GitHub Profile
@Froks15
Froks15 / rails-jsonb-queries
Created August 5, 2021 15:26 — forked from mankind/rails-jsonb-queries
Ruby on Rails-5 postgresql-9.6 jsonb queries
http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query
http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
@Froks15
Froks15 / query.sql
Created June 1, 2021 12:30 — forked from ramiroaznar/query.sql
How to find duplicate values with PostgreSQL
select * from table t1
where (select count(*) from table t2
where t1.field = t2.field) > 1
order by field
@Froks15
Froks15 / Category.rb
Created April 26, 2021 17:25
How to enforce uniqueness of the HABTM relation between category and thing on the rails side?
class Category < ActiveRecord::Base
has_and_belongs_to_many :things
validates :name, presence: true, uniqueness: true
end
# Find the latest version on https://github.com/creationix/nvm#install-script
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
# Add in your ~/.zshrc the following:
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
$ source ~/.zshrc