if you use delegate on an association that's empty, it crapxors out.
delegate :name, :to => 'monkeys.first'
is fine, but ideally i'd either
delegate :name, :to => 'monkeys.first', :unless => monkeys.first.nil?
which gets tricky due to compile-time evaluation of the unless/if clause.., or,
delegate :name, :to => 'monkeys.first', :ignore_nil => true
I know y'all will have your own preferred syntax (not really happy with any of those), so here's how I have it locally...
Index: /Volumes/rails/ba_typo/vendor/rails/activesupport/lib/active_support/core_ext/module/delegation.rb
===================================================================
--- activesupport/lib/active_support/core_ext/module/delegation.rb (revision 3811)
+++ activesupport/lib/active_support/core_ext/module/delegation.rb (working copy)
@@ -4,11 +4,11 @@
unless options.is_a?(Hash) && to = options[:to]
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key"
end
methods.each do |method|
module_eval(<<-EOS, "(__DELEGATION__)", 1)
def #{method}(*args, &block)
- #{to}.__send__(#{method.inspect}, *args, &block)
+ #{to}.__send__(#{method.inspect}, *args, &block) #{options[:ignore_nil]? "unless #{to}.nil?":""}
end
EOS
end