Skip to content

Instantly share code, notes, and snippets.

@gamov
gamov / gist:852351
Created March 3, 2011 04:55
session controller
class SessionsController < ApplicationController
skip_before_filter :login_required, :only => [:new, :create]
layout nil
def new
render #:layout => nil
end
def create
def verify_no_associated_charges
unless charges.empty?
errors.add_to_base("You cannot delete the charge code #{code} because it has associated charges")
false
else
true
end
end
def save_batches_alloc(hash)
if self.attribute_names.include?('batches_alloc')
write_attribute(:batches_alloc, hash)
else
@batches_alloc= hash
end
end
def load_batches_alloc
if self.attribute_names.include?('batches_alloc')
read_attribute(:batches_alloc)
## greatest n per group
SELECT * FROM shipping_items As t1
WHERE t1.created_at = (SELECT Max(created_at)
FROM shipping_items As t2
WHERE t2.item_variant_id = t1.item_variant_id
AND t2.item_variant_id in (11,10))
# our multi_group http://rails.lighthouseapp.com/projects/8994/tickets/120-patch-activerecord-calculations-only-accept-one-grouping-field#
# isn't updated for 2.2 yet. In the meantime, this poor man's version gives you the same results with some delimitter tom foolery.
#
# note: be careful to ensure that your group_by do not use the delimitter
# usage:
# self.multi_count(:id, :distinct => true, :group => [:symptom_id, :treatment_id])
# [[["138", "1018"], 187], [["138", "427"], 373], [["6", "1018"], 197], [["6", "427"], 393]]
class ActiveRecord::Base
def self.multi_count(field, opts = {})
fq_field_name = "#{table_name}.#{field}"
requested_items.inject(0) { |sum,it| sum += it.value } #works
# requested_items.inject() { |sum,it| sum += it.value } #not working
# requested_items.sum {|it| it.price } # not working