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
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 |
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
# 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}" |
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
## 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)) |
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
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) |
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
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 |
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 SessionsController < ApplicationController | |
skip_before_filter :login_required, :only => [:new, :create] | |
layout nil | |
def new | |
render #:layout => nil | |
end | |
def create |
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
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 |
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 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 |
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
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}"} |
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 MyModel < ActiveRecord::Base | |
serialize :preferences, Hash | |
PREFS_KEYS = [:hostname, :has_retail] | |
PREFS_KEYS.each do |key| | |
define_method(key) do | |
preferences[key] | |
end |
OlderNewer