Skip to content

Instantly share code, notes, and snippets.

select Lifestyle_cat, replace(replace(short_name, '_males', ''), '_female', '') as short_name,
Max(case when WorkOn = 'Good Work' then lifestyle_habits else '' end) as Good_Work_Feedback,
Max(case when WorkOn = 'Work on' then lifestyle_habits else '' end) as Work_On_Feedback,
Sum(case when WorkOn = 'Good Work' then ResponseCount else 0 end) as Good_Work_Count,
Sum(case when WorkOn = 'Work on' then ResponseCount else 0 end) as Work_On_Count
From (
Select
case
when q.short_name in ('binge_drinking', 'daily_drinking_female', 'daily_drinking_males')
def time_consuming(&block)
raise "Must wrapping a block" unless block_given?
t1 = Time.now
yield
t2 = Time.now
puts "Time consumed: #{t2 - t1 }"
end
def hello
count = 0
# PrinceXML Ruby interface.
# http://www.princexml.com
#
# Library by Subimage Interactive - http://www.subimage.com
#
#
# USAGE
# -----------------------------------------------------------------------------
# princely = Princely.new()
# html_string = render_to_string(:template => 'some_document')
class Fixtures
alias_method :origin_delete_existing_fixtures, :delete_existing_fixtures
def delete_existing_fixtures; puts "doing nothing."; end
end
# during the import
class Fixtures
alias_method :delete_existing_fixtures, :origin_delete_existing_fixtures
remove_method :origin_delete_existing_fixtures
end
articipantBiometric Load (0.6ms) SELECT * FROM `participant_biometrics` WHERE (`participant_biometrics`.`id` = 25984) AND (`participant_biometrics`.`deleted_at` IS NULL)
ParticipantBiometric Load (0.5ms) SELECT `participant_biometrics`.id FROM `participant_biometrics` WHERE (`participant_biometrics`.`lab_date` = '2009-01-09' AND `participant_biometrics`.user_id = 18542 AND `participant_biometrics`.deleted_at IS NULL AND `participant_biometrics`.id <> 25984) LIMIT 1
ParticipantBiometric Load (0.7ms) SELECT * FROM `participant_biometrics` WHERE (lab_date >= '2009-01-01' and lab_date < '2010-01-01') AND ((`participant_biometrics`.`deleted_at` IS NULL AND `participant_biometrics`.`user_id` = 18542))
ParticipantBiometric Load (0.5ms) SELECT * FROM `participant_biometrics` WHERE (`participant_biometrics`.`id` = 25984) AND (`participant_biometrics`.`deleted_at` IS NULL)
ParticipantBiometric Load (0.6ms) SELECT `participant_biometrics`.id FROM `participant_biometrics` WHERE (`participant_biometri
#http://refactormycode.com/codes/281-given-a-hash-of-variables-render-an-erb-template
require 'erb'
require 'ostruct'
def render_template(template, params)
vars = OpenStruct.new params
class << vars
include CustomHelper, ApplicationHelper, ActionView::Helpers::TextHelper

Instrument Anything in Rails 3

With Rails 3.0 released a few weeks ago I've migrated a few apps and I'm constantly finding useful new improvements. One such improvement is the ability to log anything in the same way that Rails internally logs ActiveRecord and ActionView. By default Rails 3 logs look slightly spiffier than those produced by Rails 2.3: (notice the second line has been cleaned up)

Started GET "/" for 127.0.0.1 at Mon Sep 06 01:07:11 -0400 2010
  Processing by HomeController#index as HTML
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE (`users`.`id` = 3) LIMIT 1

Rendered layouts/_nav.html.erb (363.4ms)

$:.unshift("/Users/miloops/Workspace/github/miloops/rails/activerecord/lib")
$:.unshift("/Users/miloops/Workspace/github/railsarel/lib")
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
ActiveRecord::Schema.define do
require 'fileutils'
start_time = Time.now
SOURCE_DB = {
:name => 'db_name',
:user => 'db_user',
:password => 'db_pass',
:host => 'localhost'
@rociiu
rociiu / webapp.rb
Created November 16, 2010 22:38 — forked from igrigorik/webapp.rb
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end