Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord composed_of doesn't work with delegated attributes and methods

#6232

If :mapping option for an aggregated attribute has mappings to a delegated attribute/method, :constructor receives "nil" instead of the expected value.

Example:

class OrderTransaction < ActiveRecord::Base
  belongs_to :order
  delegate :currency, :to => :order
  composed_of :amount
              :class_name => "Money",
              :mapping => [%w(amount cents), %w(currency currency_as_string)],
              :constructor => proc { |cents, currency| Money.new(cents, currency) }
end

Later, such order_transaction.amount.currency is always "USD" (default currency for Money). Debugging reveals that "currency" passed to the :constructor proc is "nil".
This is caused by doing explicit "read_attribute" in aggregations.rb (which doesn't take into account delegations and such). I suggest replacing "read_attribute(attr_name)" with "self.send(attr_name)".
The patch is attached.

Reported by Toyama Tokanawa · December 29th, 2010 @ 05:16 PM

State: new
Milestone: none
Assigned to: Ryan Bigg Ryan Bigg
Importance: none