views:

74

answers:

1

I am developing a Rails 1.0 application (I can't upgrade, it's a strange situation) for which I am trying to use the :composed_of functionality. I have a class called StreetAddress:

class StreetAddress
  attr_reader :address, :address2, :city, :state_id, :zip_code

  def initialize(address, address2, city, state_id, zip_code)
    @address = address
    @address2 = address2
    @city = city
    @state_id = state_id
    @zip_code = zip_code
  end


end 

and a model class called Hotel

class Hotel < ActiveRecord::Base
  composed_of :street_address
  # ...

end

which has columns:

"id", "brand_id", "code", "location_name", "address", "address2", "city", "state_id", "zip_code", "phone_number", "phone_ext", "fax_number", "time_zone", "url", "room_service_email", "manager_name", "manager_email"

However when I try to access the aggregation I get an error:

>> h = Hotel.find(1)
=> #<Hotel:0x38ad718 @attributes={"fax_number"=>"1-623-420-0124", "city"=>"Twin
Falls", "address2"=>"285", "brand_id"=>"1", "code"=>"XZWUXUSZ", "manager_email"=
>"[email protected]", "url"=>"http://www.xycdkzolukfvu.hom", "ph
one_number"=>"1-805-706-9995", "zip_code"=>"72436", "phone_ext"=>"48060", "id"=>
"1", "manager_name"=>"Igor Mcdowell", "room_service_email"=>"Duis.risus@Donecvit
ae.ca", "time_zone"=>"America/Boise", "state_id"=>"15", "address"=>"P.O. Box 457
, 7405 Dignissim Avenue", "location_name"=>"penatibus et magnis"}>
>> h.street_address
ArgumentError: wrong number of arguments (1 for 5)
    from (eval):3:in `initialize'
    from (eval):3:in `new'
    from (eval):3:in `street_address'
    from (irb):6

Why?

+1  A: 

composed_of will attempt to call the constructor of the object with the fields it knows about. In your case, it is only sending the street_address attribute (which doesn't seem to exist, so will probably be nil). Make sure to declare your composed_of with the mapping attribute set so that it will send all the attributes.

composed_of :street_address, :mapping => [%w(address address), %w(address2 address2), %w(city city), %w(state_id state_id), %w(zip_code zip_code)
Mike Dotterer
that works, but it sure seems counter to rails' "convention over configuration" philosophy. Thanks!
Tristan Havelick