Skip to content

Instantly share code, notes, and snippets.

@rob-murray
rob-murray / find_dups.rb
Created October 27, 2015 09:59
Rails find duplicate records
columns_that_make_record_distinct = [:some_id, :another_name]
distinct_ids = Model.select("MIN(id) as id").group(columns_that_make_record_distinct).map(&:id)
duplicate_records = Model.where.not(id: distinct_ids)
@rob-murray
rob-murray / install_awesome_gems
Last active December 27, 2016 11:59
Install common Ruby gems in a single command
// Useful for when you've just installed a new version of Ruby and need to install your common/favourite gems
//
gem install bundler pry awesome_print foreman rails ferver london-bike-hire-cli benchmark-ips rubocop
@rob-murray
rob-murray / cache_hashe.rb
Created September 7, 2015 13:59
Ruby cache hashe
# a method that is a simple hash acting as a cache to remote service, db call, etc
#
def a_cache
@a_cache ||= Hash.new do |cache, reference|
cache[reference] = Service.fetch(reference)
end
end
# usage
# a_cache[reference]
@rob-murray
rob-murray / rails_sql_migration.rb
Last active November 5, 2015 11:55
Rails, execute sql to update something in sql
say_with_time "Doing an update" do
execute %q{UPDATE somethings SET a_col = 'foo' WHERE other_col IN ('bar', 'foobar')}
end
# or
execute <<-SQL
CREATE UNIQUE INDEX foobars_name_lower_idx ON foobars (LOWER(name));
SQL
@rob-murray
rob-murray / sdw_butser.geojson
Last active August 29, 2015 14:26
SDW Winchester > Queen Elizabeth Country park
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rob-murray
rob-murray / money.rb
Last active January 4, 2016 11:16
example money class in Ruby (GBP)
class Money
STANDARD_VAT_RATE = 0.2.freeze
attr_reader :amount
def self.in_pence(amount_in_pence)
new(amount_in_pence)
end
def initialize(amount)
class UnionFind
attr_accessor :nodes, :sizes
def initialize(num)
self.nodes = []
self.sizes = []
num.times do |n|
self.nodes[n] = n
@rob-murray
rob-murray / cycle_1.geojson
Last active August 29, 2015 14:22
GeoJson
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rob-murray
rob-murray / docker_flow.txt
Last active June 12, 2017 00:23
docker flow; commands i run to build and deploy a container
boot2docker start
export etc
docker images
docker build -t robmurray/name:tag . # build current
docker run -P --name foo robmurray/name:tag # test
docker kill # above
docker push robmurray/name:tag
# switch to docker-machine
eval "$(docker-machine env {machine})"
docker stop current # stop current container # downtime :(
@rob-murray
rob-murray / blog_post.rb
Last active April 15, 2022 14:52
Rails STI form components conditional by type
# app/models/blog_post.rb
class BlogPost < Post
has_many :comments
end