Skip to content

Instantly share code, notes, and snippets.

@gamov
Created March 27, 2012 08:09
Show Gist options
  • Save gamov/2213902 to your computer and use it in GitHub Desktop.
Save gamov/2213902 to your computer and use it in GitHub Desktop.
Delegate fails via a :through relationship until model is saved
class ReturnedItem < AR:Base
belongs_to :sold_item
delegate :unit_price, :to => :sold_item
has_one :item_variant, :through => :sold_item
delegate :full_name, :sku, :packaging, :uom_string, :to => :item_variant
end
#item_variant is another AR model
##
#My problem is
ReturnedItem.new(:sold_item => an_saved_sold_item).full_name =>
RuntimeError: ReturnedItem.full_name delegated to item_variant.full_name, but item_variant is nil
#even though the item_variant exist
#if i save the newly record, then, it works.
# Ruby 1.9.2 / Rails 3.0.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment