This project is archived and is in readonly mode.

#6232 new
Toyama Tokanawa

ActiveRecord composed_of doesn't work with delegated attributes and methods

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

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.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages