Skip to content

Instantly share code, notes, and snippets.

@raeno
raeno / gist:8beaa77e0b5a86c2a9ec
Created October 21, 2014 06:58
Two outer joins on same table with Arel
messages = Message.arel_table
messages2 = Message.arel_table.alias('messages2')
messages3 = Message.arel_table.alias('messages3')
users = User.arel_table
manager = Arel::SelectManager.new(Message.arel_engine)
manager.project users[:first_name], users[:last_name], messages[:created_at]
manager.from messages
manager.join(messages2, Arel::Nodes::OuterJoin).on(messages[:created_at].eq(messages2[:created_at]))
manager.join(messages3, Arel::Nodes::OuterJoin).on(messages[:recipient_id].eq(messages3[:recipient_id]))
manager.join(users).on(users[:id].eq(messages[:sender_id]))
@juike
juike / restore.sql
Created November 21, 2014 06:40
Restoring a 'template1' database in PostgreSQL. (https://wiki.postgresql.org/wiki/Adventures_in_PostgreSQL,_Episode_1)
UPDATE pg_database SET datallowconn = TRUE WHERE datname = 'template0';
\c template0
UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1 WITH TEMPLATE = 'template0';
\c template1
UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
UPDATE pg_database SET datallowconn = FALSE WHERE datname = 'template0';