Skip to content

Instantly share code, notes, and snippets.

View latompa's full-sized avatar

Thomas Olausson latompa

View GitHub Profile
@latompa
latompa / gist:44489
Created January 7, 2009 22:51
update with correlated query
-- I had an existing products table, and needed to denormalize two fields for faster sorting.
-- The number of rates and total rating is currently in a ratings table, and needs to be copied to products
create table products (
id integer,
name varchar(16),
ratings_count integer,
total_rating integer
);
@latompa
latompa / bayesian rating
Created January 8, 2009 01:02
bayesian rating
-- building on data from my scenario http://gist.github.com/44489
-- and bayesian rating http://www.thebroth.com/blog/118/bayesian-rating
assume we have a products table and we want to sort products based on their rating.
With bayesian rating, you get better weighting, so that a single 5 star vote won't show up as "top product"
select * from products;
mysql> select * from products;
+------+--------+---------------+--------------+---------+
| id | name | ratings_count | total_rating | weight |
@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
-- 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),
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
require 'rubygems'
require 'activesupport'
module Fleykr
mattr_accessor :token
def self.search_photos
p "searching, and my token is #{Fleykr.token}"
end
end
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"}]
#
# 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'
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
@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",