[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rails question - ActiveRecord::Base#composed_of

Dave Burt

2/12/2005 1:05:00 PM

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


3 Answers

David Heinemeier Hansson

2/12/2005 3:20:00 PM

0

> 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/sho...
--
David Heinemeier Hansson,
http://www.basec... -- Web-based Project Management
http://www.rubyon... -- Web-application framework for Ruby
http://www.loudthi... -- Broadcasting Brain



Dave Burt

2/12/2005 3:38:00 PM

0

"David Heinemeier Hansson" <david@loudthinking.com> wrote:
>> 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/sho...

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


James Britt

2/12/2005 7:23:00 PM

0

Dave Burt wrote:
> "David Heinemeier Hansson" <david@loudthinking.com> wrote:
>
>>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/sho...
>
>
> 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.


Gmane gateways vast numbers of OSS-related mailing lists to Usenet
groups, which are also viewable via the Web


news.gmane.org

http://news.gmane.org/gmane.comp.lang.ruby.rails/c...


James