Michael
Michael

Reputation: 25

Ruby Data_Mapper resource not able to be saved - expecting different data type

I'm attempting to save some information relating to real estate in my local area.

I'm using Ruby with the Data_Mapper gem to persist the data to a local MySQL database.

The models currently look like thus:

    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

I'm able to create and save Properties and States, however when I attempt to create a Suburb I get the following error:

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

Is this error because I am also not defining a State when creating the object? I've tried different data types for the properties but I still receive the same error. The only thing I take away from this is possibly because I have a belongs_to and has_many relationship?

Any help is greatly appreciated!

Upvotes: 0

Views: 78

Answers (2)

Michael
Michael

Reputation: 25

The issue was with the spelling of Properties when referencing it in the model for Suburbs. The correct spelling (according to Ruby) is Propertys.

Upvotes: 1

Vidya
Vidya

Reputation: 30310

Have you tried creating a Suburb by adding it to State's suburbs collection?

Assuming state has been created:

suburb = Suburb.new(:name => "Test", :post_code => 4321)
state.suburbs << suburb
state.save

Upvotes: 0

Related Questions