Skip to content

Instantly share code, notes, and snippets.

@mrzasa
mrzasa / devices.rb
Last active October 13, 2016 14:50
ActiveRecord::AssociationRelation vs ActiveRecord::Associations::CollectionProxy
> user.devices.all.uniq.class
=> Device::ActiveRecord_AssociationRelation
# so that we can chain more AR methods
> user.devices.all.uniq.limit(1)
=> [Device]
> u.devices.uniq.class
=> Array
# so we can only work on array:
> user.devices.uniq.limit(1)
@gabeodess
gabeodess / custom_arel.rb
Last active September 20, 2020 23:16
An example of how to add a custom predicate to Arel
module Arel::Predications
def has_key(right)
Arel::Nodes::HasKey.new(self, quoted_node(right))
end
end
class Arel::Nodes::HasKey < Arel::Nodes::Binary
def operator; :"?" end
end
@brianhempel
brianhempel / bench_rails_memory_usage.rb
Last active August 28, 2025 07:07
A script to test the memory usage of your Rails application over time. It will run 30 requests against the specified action and report the final RSS. Choose the URL to hit on line 45 and then run with `ruby bench_rails_memory_usage.rb`.
require "net/http"
def start_server
# Remove the X to enable the parameters for tuning.
# These are the default values as of Ruby 2.2.0.
@child = spawn(<<-EOC.split.join(" "))
XRUBY_GC_HEAP_FREE_SLOTS=4096
XRUBY_GC_HEAP_INIT_SLOTS=10000
XRUBY_GC_HEAP_GROWTH_FACTOR=1.8
XRUBY_GC_HEAP_GROWTH_MAX_SLOTS=0
@DTFagus
DTFagus / analyse_watchers.js
Created August 12, 2014 14:22
Bookmarklet to analyse angular watchers
javascript: (function() {
var root = $(document.getElementsByTagName('html'));
var watchers = [];
var attributes = [];
var attributes_with_values = [];
var elements = [];
var elements_per_attr = [];
var scopes = [];
@xdite
xdite / 42-things.md
Created July 14, 2012 05:11
Ten (42) Things You Didn't Know Rails Could Do