Ruby Data_Mapper资源无法保存 - 期望不同的数据类型

时间:2013-10-10 04:17:47

标签: mysql ruby datamapper ohm

我正试图在当地保存一些与房地产有关的信息。

我正在使用带有Data_Mapper gem的Ruby将数据持久保存到本地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关系?

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您是否尝试通过将其添加到Suburb的{​​{1}}集合来创建State

假设已创建suburbs

state