0

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!

Michael
  • 33
  • 5
  • Whats interesting, when I create a new State I get the following output: `=> #` Yet when I create a Suburb I get: `=> #` – Michael Oct 10 '13 at 04:48

2 Answers2

1

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

Michael
  • 33
  • 5
0

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
Vidya
  • 29,932
  • 7
  • 42
  • 70
  • I haven't tried that, but I still receive the same error: `state.name = "TEST" => "TEST" irb(main):036:0> state.abbreviation="TEST" => "TEST" irb(main):037:0> state.save ~ (0.000167) SET sql_auto_is_null = 0 ~ (0.000098) SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL' ~ (0.002509) INSERT INTO `states` (`name`, `abbreviation`) VALUES ('TEST', 'TEST') => true` `suburb = Suburb.new(:name => "test", :post_code => 4321, :state=>state) ArgumentError: arguments may be 1 or 2 Integers, or 1 Range object, was: [:name]` – Michael Oct 10 '13 at 04:38