This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 | |
); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'activesupport' | |
module Fleykr | |
mattr_accessor :token | |
def self.search_photos | |
p "searching, and my token is #{Fleykr.token}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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", |
OlderNewer