Put this on 
StackOverflow<http://stackoverflow.com/questions/15044787/ruby-datamapper-one-to-many-association-wont-save-using-parent-key>,
 
but figured I would ask here as well.

I have setup the following model with specific parent and child keys:

    class Province
      include DataMapper::Resource

      property :name_short, String, key: true, length: 2, unique: true
      property :name_long,  String, length: 1..50

      has n, :municipalities, 'Municipality',
        parent_key: [:name_short],
        child_key: [:province]
    end

    class Municipality
      include DataMapper::Resource

      property :province,            String, key: true, length: 2
      property :name,                String, key: true, length: 1..40
      property :lat,                 Float
      property :long,                Float

      property :current_population,  Integer

      belongs_to :province, 'Province',
        parent_key: [:name_short],
        child_key: [:province]

    end

I then create the associated record with:

            province = Province.get('BC')
            municipality = province.municipalities.new(
              name: '100 mile house',
              lat: 51.23131,
              long: 121.65489,
              current_population: 0)

And finally execute `municipality.save`, which fails because the record it 
is trying to save (see below) is trying to use the entire *Province object*as 
the key, 
*instead of just the :name_short* field.

    #<Municipality @province=#<Province @name_short="BC" 
@name_long="British Columbia"> @name="100 mile house" @lat=51.64300975 
@long=121.295022 @current_population=0>

The `save` error returned is a validation type error:

    ["Province must be at most 2 characters long", "Province must be of 
type String"]

As well, after trying a few thing, adding the following `to_s` method to 
the `Province` class, managed to get rid of the `"Province must be at most 
2 characters long"` error - but still get the String error:

    def to_s
      @short_name
    end

I tried removing the parent/child_key: options, to let DM build its own 
association key fields, but that didn't work either.jjj


I must admit, I'm at a loss, and have started taking a look how to do this 
with Sequel. Since I've spent the last couple weeks invested in trying to 
get DM working with my projects, I'd rather not see that time go to was and 
would appreciate some help.

P.s. Is DataMapper still being developed and supported somewhere? It really 
seems hard to find any sort of help, except for the rare blog entry or 
occasional answer on StakOverflow.

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to datamapper+unsubscr...@googlegroups.com.
To post to this group, send email to datamapper@googlegroups.com.
Visit this group at http://groups.google.com/group/datamapper?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to