Rails question - ActiveRecord::Base#composed_of

D

Dave Burt

Hi,

Can any Rails gurus help me out with this? I'm using Rails 0.9.5, and have a
model like so:

class Person < ActiveRecord::Base
composed_of :family,
:mapping => [%w(family_name name), %w(family_phone phone),
%w(family_address address)]
end

But I don't get Person#family_name et. al.:

NoMethodError in Person#index
Showing /person/list.rhtml where line #18 raised "undefined method
`family_name' for #<Person:0x2b27e28>"

The doco on ActiveRecord::Aggregations::ClassMethods says:

(begin quote)

Example:
class Customer < ActiveRecord::Base
composed_of :balance, :class_name => "Money", :mapping => %w(balance
amount)
composed_of :address, :mapping => [ %w(address_street street),
%w(address_city city) ]
end

The customer class now has the following methods to manipulate the value
objects:
Customer#balance, Customer#balance=(money)
Customer#address, Customer#address=(address)

(end quote)

If it's not working as documented, is there a simple alternative?

Thanks for any help,
Dave
 
D

David Heinemeier Hansson

class Person < ActiveRecord::Base
composed_of :family,
:mapping => [%w(family_name name), %w(family_phone
phone),
%w(family_address address)]
end

This setup expects that your people table has family_name and
family_phone as columns. The aggregation works by presenting multiple
fields as one object, so when you do have these columns, you should be
able to do: person.family.name and person.family.phone.

I'd say this approach is only worth it if you have considerable or very
convenient business logic tied to your Family object, though. If it's
just data, I'd much rather just save the trouble and do
person.family_name.

Hint: For faster and more complete assistance, I recommend signing up
for the Rails mailing list or visiting IRC. More information on
http://wiki.rubyonrails.com/rails/show/HomePage
 
D

Dave Burt

David Heinemeier Hansson said:
class Person < ActiveRecord::Base
composed_of :family,
:mapping => [%w(family_name name), %w(family_phone phone),
%w(family_address address)]
end

This setup expects that your people table has family_name and family_phone
as columns. The aggregation works by presenting multiple fields as one
object, so when you do have these columns, you should be able to do:
person.family.name and person.family.phone.

Oh, I see. I was hoping for the reverse - Family has_many :people, but I'd
like to consider the Family record pretty much as a part of any given
Person.

I suppose I can add my own methods to
ActiveRecord::Aggregations::ClassMethods though? I can't think of a name
right now, though; comprised_of seems wrong.
Hint: For faster and more complete assistance, I recommend signing up for
the Rails mailing list or visiting IRC. More information on
http://wiki.rubyonrails.com/rails/show/HomePage

I'm on Usenet here - the Rails ML doesn't map to a newsgroup, does it? And I
did try IRC, but it was kinda quiet.

Anyway, thanks for the help. And the framework, it's sick, despite this
particular method doing what I want backwards :)

Cheers,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top