Skip to content

Instantly share code, notes, and snippets.

charly / document.rb
Last active Jan 16, 2018
How to join associated models of a PgSearch Document to apply dynamic filter
View document.rb
class PgSearch::Document
belongs_to :article, -> {where("searchable_type = 'Article'")},
foreign_key: 'searchable_id'
belongs_to :info, -> {where("searchable_type = 'Info'")},
foreign_key: 'searchable_id'
@searched = PgSearch.multisearch(@query).
charly / gist:89bca978b6f9c0154c4c
Created Oct 21, 2014
One Script ActiveRecord Bug Report (from rails)
View gist:89bca978b6f9c0154c4c
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.0'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
View gist:8114471
require 'matrix'
def regression x, y, degree
x_data = {|xi| ({|pow| (xi**pow) }}
mx = Matrix[*x_data]
my = Matrix.column_vector y
((mx.t * mx).inv * mx.t * my).transpose.to_a[0].reverse
View gist:7819514

Combining Scopes & Ransack thanks to Virtus & Siphon

There's a common feature request on the [ransack][2] issue page and that's to integrate your usual ActiveRecord scopes within the search. Indeed ransack is a fantastic tool to quickly setup a form for selecting table rows depending on column values. But it falls a little short when you want more complex, relational searches. So you almost naturally add a scope in your ransack powered search form before you realize it doesn't work and there's no clear way to do it.

Ransack won't take it all

The problem with that attractive and somewhat obvious feature request is [ransack][2] relies on the typecasting activerecord performs on its attributes, to coerce all the string values of the parameter's hash into their real value. When params[:user][:age_gt] => "18" barges into your model it knows "18" should be an Integer (because the corresponding DB column says so) and will pursue accordingly.

On the flip side your model has no reason to know what's the ar

charly / uploader_fu.rb
Created Mar 8, 2011
a nice little module to help migrate from attachment_fu to carrierwave
View uploader_fu.rb
# Helps you migrate from attachment_fu
# put it in your /lib dir and include it your xxxx_uploader.rb
module UploaderFu
def partition_dir
("%08d" %\d{4}/).join("/")
def model_dir
View noproxy-association.rb
module Mongoid
def self.included(base)
base.extend NoProxyAssociation
# This little piece of codes creates an author method in Book
# which instanciates an anonymous class with Author included!
# def author
# klass = do
# include Book::Author
View depth_first_tree_traversal.rb
require "rubygems"
require "xml/libxml"
#require "active_support"
def traverse(node)
children = {|a| a.element?}
txt = "<h1>HTML output</h1><ul>"
last_depth, depth = 1, 1
View module_for_plugin.rb
class Asset < ActiveRecord::Base
acts_as_taggable_on :tags, :people
def tag_list=(new_tags)
def person_list=(new_tags)
View refactoring_rails_err.rb
module MyTransaction
def save
Transaction.instance_eval do
@@save = instance_method(:save)
remove_method :save
body = "my transaction! + " + super
Transaction.module_eval do
View rails_refactoring.rb
require "rubygems"
require "uninclude"
module Core
def save; "saved" end
module Dirty
def save; "clean + " + super end