Skip to content

Instantly share code, notes, and snippets.

View yurko's full-sized avatar

Yurko Bregey yurko

View GitHub Profile
@mehdi-farsi
mehdi-farsi / habtm_hmt.md
Last active February 5, 2023 10:32
Replace HABTM by HMT with Join Table as Model

Has And Belongs To Many (use case)

how it works ?

Let's take the simple example of a blog with:

  • A post habtm tags
  • A tag habtm posts
@mankind
mankind / rails-jsonb-queries
Last active May 23, 2024 06:47
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")
@ostinelli
ostinelli / reloader.rb
Last active July 1, 2018 08:18
Reload an initializer file on every request in Rails.
# add this into application.rb
initializer_file = Rails.root.join('config', 'initializers', 'my_initializer.rb')
reloader = ActiveSupport::FileUpdateChecker.new([initializer_file]) do
load initializer_file
end
ActiveSupport::Reloader.to_prepare do
reloader.execute
end
@davidderus
davidderus / .dockerignore
Last active March 3, 2024 10:15
Docker + Rails + Puma + Postgres + Nginx
.git
.gitignore
/doc
.yardoc
coverage
jsdoc
/tmp
/log
Dockerfile
Dockerfile.prod
@ohthatjames
ohthatjames / gist:60a0b219443aeeb6cb41
Last active May 21, 2019 13:15
Registering to_procs without monkeypatching everything
require "set"
class Proc
def self.register_type(klass, proc)
@types_to_convert ||= {}
@types_to_convert[klass] = proc
end
def self.[](field)
-> (x) { (@types_to_convert || {})[field.class].call(field, x) }
  • Dynamic Dispatch
  • Dynamic Method
  • Ghost Methods
  • Dynamic Proxies
  • Blank Slate
  • Kernel Method
  • Flattening the Scope (aka Nested Lexical Scopes)
  • Context Probe
  • Class Eval (not really a 'spell' more just a demonstration of its usage)
  • Class Macros