0

我正在尝试保存一些与我所在地区的房地产有关的信息。

我正在使用 Ruby 和 Data_Mapper gem 将数据保存到本地 MySQL 数据库。

这些模型目前看起来像这样:

    class Property
    include DataMapper::Resource

    property :id, Serial
    property :num, String
    property :street, String
    property :street_type, String
    property :price, String
    property :block_size, String
    property :unimproved_value, String
    property :found, DateTime
    property :last_seen, DateTime

    belongs_to :suburb

end

class Suburb
    include DataMapper::Resource

    property :id,         Serial
    property :name, String
    property :post_code, Integer

    has n, :properties
    belongs_to :state

end

class State
    include DataMapper::Resource

    property :id,         Serial
    property :name, String
    property :abbreviation, String

    has n, :suburbs
end

我能够创建和保存属性和状态,但是当我尝试创建郊区时,我收到以下错误:

irb(main):006:0> Suburb.create(:name => "Test", :post_code => 4321)
ArgumentError: arguments may be 1 or 2 Integers, or 1 Range object, was: [:name]
    from /var/lib/gems/1.9.1/gems/dm-core-1.2.1/lib/dm-core/collection.rb:390:in `[]'
    from /var/lib/gems/1.9.1/gems/dm-core-1.2.1/lib/dm-core/model/property.rb:236:in `name='
    from /var/lib/gems/1.9.1/gems/dm-core-1.2.1/lib/dm-core/resource.rb:336:in `block in attributes='
    from /var/lib/gems/1.9.1/gems/dm-core-1.2.1/lib/dm-core/resource.rb:332:in `each'
    from /var/lib/gems/1.9.1/gems/dm-core-1.2.1/lib/dm-core/resource.rb:332:in `attributes='
    from /var/lib/gems/1.9.1/gems/dm-core-1.2.1/lib/dm-core/resource.rb:755:in `initialize'
    from /var/lib/gems/1.9.1/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in `new'
    from /var/lib/gems/1.9.1/gems/dm-validations-1.2.0/lib/dm-validations.rb:129:in `create'
    from (irb):6
    from /usr/bin/irb:12:in `<main>'

这个错误是因为我在创建对象时也没有定义状态吗?我为属性尝试了不同的数据类型,但仍然收到相同的错误。我唯一能从中得到的可能是因为我有一个 belongs_to 和 has_many 关系?

任何帮助是极大的赞赏!

4

2 回答 2

1

Properties问题在于在郊区模型中引用它时的拼写。正确的拼写(根据 Ruby)是Propertys.

于 2013-10-10T04:55:12.600 回答
0

您是否尝试过Suburb通过将其添加到Statesuburbs集合来创建一个?

假设state已创建:

suburb = Suburb.new(:name => "Test", :post_code => 4321)
state.suburbs << suburb
state.save
于 2013-10-10T04:26:56.757 回答