#212 √ wontfix
Chris Hapgood

Add alternate constructor support to aggregations

Reported by Chris Hapgood | May 16th, 2008 @ 10:15 PM

The composed_of macro assumes that the value object can be instantiated with new. But for some aggregate classes, new is not the appropriate constructor and instead something like parse is required.

This patch adds support for a :constructor option to the composed_of method. It accepts a symbol identifying a constructor method on the aggregate class. All aggregate object are then instantiated using this constructor method.

A typical example of this new functionality:

require 'uuid'

class Widget < ActiveRecord::Base
  composed_of :uuid, :constructor => :parse
end

Of course, the default value for the constructor is :new.

[This is identical in function to the enhancement documented on Trac ticket 11122. It has been updated to work on Edge and includes less brittle tests and a git-compatible patch.]

Comments and changes to this ticket

  • Pratik

    Pratik July 1st, 2008 @ 11:44 PM

    • → Tag changed from “” to “activerecord edge enhancement patch tests”
    • → State changed from “new” to “wontfix”

    You can just call parse from #initialize itself. Not convinced about the provided use case for this.

    Thanks.

  • Chris Hapgood

    Chris Hapgood July 2nd, 2008 @ 03:24 PM

    Agreed, you can call parse from MyARModel#initialize. But that seems like a reason for scrapping aggregations entirely because you could also instantiate the composed_of objects in #initialize as well.

    I would be happy to see aggregations disappear entirely -they seem like a very thin veneer on top of existing Ruby/AR constructors. But if they are worth keeping, why not make them useful for the large population of classes that use constructors like parse and from_x? The patch I propose is pretty trivial...

  • Pratik

    Pratik July 2nd, 2008 @ 03:41 PM

    If we have enough people asking for this, we can surely roll it in.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

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

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins

People watching this ticket