Skip to content

Instantly share code, notes, and snippets.

View latompa's full-sized avatar

Thomas Olausson latompa

View GitHub Profile
@latompa
latompa / find duplicate records
Created October 30, 2009 02:29
find duplicate records
SELECT name,
COUNT(name)
FROM users
GROUP BY name
HAVING ( COUNT(name) > 1 )
@latompa
latompa / gist:217325
Created October 24, 2009 01:38
find users with no activity
-- members
create table members (id integer, age integer);
insert into members (id,age) values
(1,10) ,(2,20) ,(3,30) ,(4,25) ,(5,28) ,(6,37) ,(7,38) ,(8,68) ,(9,9)
-- posts
create table posts (id integer, user_id integer, title varchar(32))
insert into posts (id,user_id,title) values
@latompa
latompa / count in buckets
Created October 23, 2009 21:27
count in buckets
-- table
create table members (id integer, age integer);
-- data
insert into members (id,age) values
(1,10), (2,20), (3,30), (4,25), (5,28), (6,37), (7,38), (8,68), (9,9);
-- query
select
SUM(case when age between 1 and 20 then 1 else 0 end) as "1-20",
You have an audit table
created | action
---------------------+--------
2009-01-01 00:00:00 | create
2009-01-02 00:00:00 | delete
2009-01-15 00:00:00 | create
2009-01-16 00:00:00 | create
and you want a report with two counts, created and deleted for each week
#
# In the following test, the setup is supposed to create a variable,
# but fails because of a missing method
#
# If you run this test, you wont see the setup error,
# it's being masked by teardown, because @some_object is nil
#
require 'test/unit'
require 'rubygems'
require 'rubygems'
require 'lib/fleakr'
Fleakr.api_key = 'API_KEY'
Fleakr.shared_secret = 'SECRET'
MAX_TRIES = 100
user_list = [{:auth_token => "TOKEN_1", :owner_id => "USER_1"},
{:auth_token => "TOKEN_2", :owner_id => "USER_2"}]
require 'rubygems'
require 'activesupport'
module Fleykr
mattr_accessor :token
def self.search_photos
p "searching, and my token is #{Fleykr.token}"
end
end
class Account < ActiveRecord::Base
has_many :balance_transfers
has_many :orders
def purchase_order(description, cost)
begin
transaction do
balance_transfers.create! :change=>cost
orders.create! :description=>description
end
-- You have a products table, and want to show the ones a user can afford first sorted on price
-- followed by the products he can't afford, also sorted
create table products(id integer, name varchar(32), price integer)
insert into products values
(1,"teddy bear",10),
(2,"red fire engine",5),
(3,"doll house",50),
(4,"lego castle",190),
@latompa
latompa / gist:45288
Created January 9, 2009 21:31
get all keys from a attachment_fu s3 table
You are using attachment_fu and have a database like so
id product_id parent_id content_type thumbnail filename
1 500 (null) image/png (null) ipod.png
2 (null) 1 image/png medium ipod_medium.png
3 (null) 1 image/png large ipod_large.png
and you want to produce a list of all your s3 urls (for copying a bucket maybe)
product_images/1/ipod.jpg