Skip to content

Instantly share code, notes, and snippets.

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
# 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}"
## 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))
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)
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
@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
@gamov
gamov / gist:1011864
Created June 7, 2011 08:01
Model Optimisation
def any_missing_specs
if item_variants.loaded?
item_variants.any?{|si| si.m3 == 0 || si.weight == 0}
else
item_variants.where({:m3 => 0 } | {:weight => 0}).size != 0 #metawhere
end
end
@gamov
gamov / gist:1041878
Created June 23, 2011 04:05
Getting a different model from an association
class Shipment
has_many :shipping_items, :inverse_of => :shipment, :dependent => :destroy do
def linked_po_ids
joins(:purchased_item => :purchase_order).select('DISTINCT purchase_orders.id AS po_id').map{|si| si.po_id}
def linked_pos
? #how to return the purchase orders (objects) ?
end
end
def linked_pos
def update_deliver_partially
@purchase_order = PurchaseOrder.find(params[:id], :include => {:purchased_items => {:item_variant => :item_family}})
# authorize! :update
@purchase_order.attributes = params[:purchase_order]
@stock_receipt = @purchase_order.stock_receipts.to_a.find{|sr| sr.new_record?}
#logger.debug @stock_receipt.inspect
# @purchased_items.each {|pi| p "in controller #{pi.wh_alloc_hash.inspect}"}
@gamov
gamov / gist:1286351
Created October 14, 2011 05:55
Adding setter and getter of attributes of a AR model backed by an Hash
class MyModel < ActiveRecord::Base
serialize :preferences, Hash
PREFS_KEYS = [:hostname, :has_retail]
PREFS_KEYS.each do |key|
define_method(key) do
preferences[key]
end