Skip to content

Instantly share code, notes, and snippets.

@paradigm314
paradigm314 / model.rb
Created July 12, 2017 18:40
Rails Relationship Methods
has_many :organizations, through: :user_roles do
def default
where(:default_organization_id => self.id).first
end
end
@paradigm314
paradigm314 / compose_actions.js
Created June 13, 2017 19:17
Compose Redux Thunk Action Creators
// If you use Redux Thunk...
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
const store = createStore(reducer, applyMiddleware(thunk))
// You can define asynchronous action creators that return functions.
// We call such action creators "thunks":
export function getUser(id) {
// Redux Thunk will inject dispatch here:
@paradigm314
paradigm314 / duplicates.sql
Last active December 19, 2017 16:47
Remove Duplicates Postgres
/* Single Key Filter */
SELECT * FROM users
WHERE id IN (SELECT id
FROM (SELECT id, ROW_NUMBER()
OVER (PARTITION BY email ORDER BY id) AS rnum
FROM users) t
WHERE t.rnum > 1);
/* Multi Key Fileter */
SELECT * FROM theme_files
@paradigm314
paradigm314 / RecursiveObjectPropertyTrace.js
Created May 17, 2017 16:07
Traces a JS object recursively following an array path of keys.
function objectPropertyTrace(object, path){
let key = path.shift();
let o = object[key];
if(path.length === 0 || o === undefined || path[0] === ""){
return o;
} else {
return objectPropertyTrace(o, path);
}
}
@paradigm314
paradigm314 / console.rb
Created September 7, 2016 20:26
Rails 4 generate scaffolding in admin namespace
# Run In Console
rails g model Foo
rails g scaffold_controller Admin::Foo --model-name=Foo
# Generates
# db/migrate/20160907201951_create_foo.rb
# app/models/foo.rb
# app/controllers/admin/foo_controller.rb
# app/views/admin/foo
# app/views/admin/foo/index.html.erb