Skip to content

Instantly share code, notes, and snippets.

require 'rubygems'
gem 'dm-core', '0.10.0'
require 'dm-core'
DataMapper.setup(:default, 'postgres://tpitale@localhost/test_through')
# DataObjects::Postgres.logger = DataObjects::Logger.new(STDOUT, :debug)
class Retailer
include DataMapper::Resource
INSERT INTO "sales" ("sale_time", "retailer_id", "user_id") VALUES ('2009-05-14 12:44:46.474-04', 1, 1364) RETURNING "id"
Wed, 10 Jun 2009 20:22:09 GMT ~ debug ~ (0.006935) SELECT "id", "retail", "points", "sale_id", "item_id" FROM "sale_items" WHERE "sale_id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 17
> ratings = (1..21).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
> ratings.map { ratings.slice!(0,2) }
=> [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13, 14]]
> ratings
=> [15, 16, 17, 18, 19, 20, 21]
# why are those left over?
-- query for user.items, which is User => sales => sale_items => item
SELECT "items"."id", "items"."item_code", "items"."item_number", "items"."description", "items"."size", "items"."retail", "items"."vintage"
FROM "items"
INNER JOIN "sale_items"
ON "items"."id" = "sale_items"."item_id"
INNER JOIN "sales"
ON "sale_items"."sale_id" = "sales"."id"
INNER JOIN "users"
ON "sales"."user_id" = "users"."id"
require 'rubygems'
gem 'dm-core', '0.10.0'
require 'dm-core'
DataMapper.setup(:default, 'postgres://tpitale@localhost/test_through_many')
class User
include DataMapper::Resource
property :id, Serial
has n, :sales
@tpitale
tpitale / gist:136078
Created June 25, 2009 19:03
Failed create!
require 'rubygems'
gem 'dm-core', '0.10.0'
gem 'dm-validations', '0.10.0'
require 'dm-core'
require 'dm-validations'
DataMapper.setup(:default, 'postgres://tpitale@localhost/test_create_fail')
class User1
include DataMapper::Resource
@tpitale
tpitale / gist:136687
Created June 26, 2009 19:41
jQuery clear on focus
;(function($) {
$.fn.clearfocus = function() {
return this.each(function() {
var $field = $(this);
var original_value = $field.val();
$field.focus(function() {
if($field.val() == original_value) $field.val("");
});
require 'rubygems'
require 'dm-core'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite3::memory:')
class Session
include DataMapper::Resource
property :id, Serial
>> Item.first(:retailer_id => r.id)
ArgumentError: condition :retailer_id does not map to a property or relationship in Item
from /var/www/apps/tasty/releases/20090708133514/vendor/gems/dm-core-0.10.0/lib/dm-core/query.rb:747:in `assert_valid_conditions'
from /var/www/apps/tasty/releases/20090708133514/vendor/gems/dm-core-0.10.0/lib/dm-core/query.rb:743:in `each'
from /var/www/apps/tasty/releases/20090708133514/vendor/gems/dm-core-0.10.0/lib/dm-core/query.rb:743:in `assert_valid_conditions'
from /var/www/apps/tasty/releases/20090708133514/vendor/gems/dm-core-0.10.0/lib/dm-core/query.rb:670:in `assert_valid_options'
from /var/www/apps/tasty/releases/20090708133514/vendor/gems/dm-core-0.10.0/lib/dm-core/query.rb:660:in `each'
from /var/www/apps/tasty/releases/20090708133514/vendor/gems/dm-core-0.10.0/lib/dm-core/query.rb:660:in `assert_valid_options'
from /var/www/apps/tasty/releases/20090708133514/vendor/gems/dm-core-0.10.0/lib/dm-core/query.rb:602:in `initialize'
from /var/www/apps/tasty/releases/2009
cookies = document.cookie.split(';');
utmz = "";
for(i in cookies) {
if(cookies[i].test(/^__utmz/)) utmz = cookies[i];
}
medium = utmz.match(/".*?utmcmd=([a-z]+)\|/)[1];
adwords = utmz.match(/.*?utmgclid=([_0-9A-Za-z]+)\|/)[1];