Rails 1.0 - 使用composed_of 给我错误的参数数量(1 for 5)错误

发布于 2024-08-27 09:15:54 字数 1821 浏览 6 评论 0原文

我正在开发一个 Rails 1.0 应用程序(我无法升级,这是一个奇怪的情况),我尝试使用 :composed_of 功能。我有一个名为 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 

和一个名为 Hotel 的模型类,

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

end

其中包含 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"

但是,当我尝试访问聚合时,出现错误:

>> 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

为什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-09-03 09:15:54

composed_of 将尝试使用它所知道的字段来调用对象的构造函数。在您的情况下,它仅发送 street_address 属性(该属性似乎不存在,因此可能为零)。确保使用映射属性集声明您的composed_of,以便它将发送所有属性。

composed_of :street_address, :mapping => [%w(address address), %w(address2 address2), %w(city city), %w(state_id state_id), %w(zip_code zip_code)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文