Skip to content

Instantly share code, notes, and snippets.

class Foo < ActiveRecord::Base
has_many :bars, :include => :baz, :conditions => "bazes.active = 1"
has_many :bazes, :through => :bars
end
class Bar
belongs_to :foo
belongs_to :baz
end
@emschwar
emschwar / gist:1158032
Created August 19, 2011 21:19
Named scopes that re-use other named scopes via proxy_options
class Parent < ActiveRecord::Base
named_scope :ready, :conditions => { :ready_at => true }
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
named_scope :ready, lambda {
:joins => :parent,
@emschwar
emschwar / gist:1150242
Created August 16, 2011 21:38
Transactions across before :all with rspec2
require 'spec_helper'
describe "transactions" do
before :all do
@device = Factory(:device)
end
it "has one device before a nested context" do
Device.count.should == 1
end
Research.FormattedListItemView = SC.ListItemView.extend({
renderLabel: function(context, label) {
var del = this.displayDelegate,
formatter = this.getDelegateProperty("contentFormatter", del);
label = (formatter && label) ? formatter(label) : label;
context.push('<label>', label || '', '</label>') ;
},
toolTip: function() {
var content = this.get('content'),
del = this.displayDelegate,
Todos.Task = SC.Record.extend(
/** @scope Todos.Task.prototype */ {
isDone: SC.Record.attr(Boolean),
description: SC.Record.attr(String)
}) ;
Todos.store.find(Todos.Task).firstObject().get('attributes') => Object guid=task-1
CREATE AGGREGATE array_accum (anyelement)
(
sfunc = array_append,
stype = anyarray,
initcond = '{}'
);
SELECT op.uuid AS ordered_placement_id,
SUM(die1.impressions) AS impressions,
SUM(die1.clicks) AS clicks,
SELECT agencies.name
FROM agencies
INNER JOIN manifests ON manifests.agency_id = agencies.id
INNER JOIN plans ON plans.manifest_id = manifests.id
INNER JOIN plan_rows ON plan_rows.plan_id = plans.id
INNER JOIN placements ON placements.plan_row_id = plan_rows.id
INNER JOIN ordered_placements ON placements.ordered_placement_id = ordered_placements.id
WHERE ordered_placement_id = 87952
AND plans.id = MAX(plans.id);
SELECT manifests.campaign_id,SUM(delivery_item_entries.impressions)
FROM delivery_item_entries
INNER JOIN delivery_items on delivery_item_entries.delivery_item_id = delivery_items.id
INNER JOIN third_party_placements on delivery_items.third_party_placement_id = third_party_placements.id
INNER JOIN ordered_placements on third_party_placements.ordered_placement_id = ordered_placements.id
INNER JOIN placements on ordered_placements.id = placements.ordered_placement_id
INNER JOIN plan_rows on placements.plan_row_id = plan_rows.id
INNER JOIN plans on plan_rows.plan_id = plans.id
INNER JOIN manifests on plans.manifest_id = manifests.id
def self.find_for_centro_finance(id)
Closing.find id,
:conditions => "third_party_placements.ordered_placement_id IS NOT NULL",
:include => [
:delivery_item_entries,
:delivery_item,
:third_party_placement,
:ordered_placement,
:placements,
:plan_row,
app/models/ug 10 11:48:25 narsil rails[19142]: ActionView::TemplateError (You have a nil object when you didn't expect it!The error occurred while evaluating nil.campaign_id) on line #4 of app/views/closings/show.xml.builder:1: xml.closing(:period_name => @closing.period_name) do2: xml.manifests(:type => "array") do3: @closing.receivables.each do |receivable|4: xml.manifest(:campaign_id => receivable.campaign_id,5: :advertiser => receivable.advertiser.name,6: :agency => receivable.agency.name) do7: xml.plan_rows(:type => "array") do (__DELEGATION__):2:in `__send__' (__DELEGATION__):2:in `campaign_id' app/views/closings/show.xml.builder:4:in `_run_builder_app47views47closings47show46xml46builder' app/views/closings/show.xml.builder:3:in `each' app/views/closings/show.xml.builder:3:in `_run_builder_app47views47closings47show46xml46builder' builder (2.1.2) lib/builder/xmlbase.rb:134:in `call' builder (2.1.2) lib/builder/xmlbase.rb:134