Pezholio
Pezholio

Reputation: 2694

Mongomapper Foreign Key?

I'm currently using Mongomapper within a Padrino project where I'm importing data from an external source. The main object (Application) has two associated document types, Activity and Notice.

However, I'd like to specify the foreign key myself, rather than use the internal Mongo ID, as the foreign key is in the data export, which I import via a rake task.

I've tried an EmbeddedDocument, but this causes issues as I have to delete all the associated data, rather than updating it, which isn't ideal.

I've tried the following, but without any luck:

class Application
  include MongoMapper::Document
  ensure_index [[:latlng, '2d']]

  key :refval, String
  key :pkeyval, String
  key :applicantname, String
  key :latlng, Array
  key :address, String
  key :occupier, String
  key :type, String
  key :casetype, String
  key :tradingname, String
  key :closingdate, Date
  key :recieveddate, Date
  key :details, String
  key :usetype, String
  key :status, String
  key :validfrom, Date
  timestamps!

  many :activities
  many :notices  
end

class Activity
  include MongoMapper::Document

  key :keyval, String
  key :pkeyval, String
  key :type, String  
  key :cycle, String
  key :open, String
  key :close, String

  belongs_to :application, :foreign_key => :pkeyval
end

class Notice
  include MongoMapper::Document

  key :keyval, String
  key :pkeyval, String
  key :recieveddate, Date
  key :startdate, Date
  key :enddate, Date
  key :days, String  
  key :hours, String
  key :activities, Array

  belongs_to :application, :foreign_key => :pkeyval
end

Any ideas where I'm going wrong?

Upvotes: 1

Views: 530

Answers (1)

jmikola
jmikola

Reputation: 6922

MongoMappers's Associations documentation is a bit light on this, but there's an example to be found in the test_associations functional test. The :foreign_key definition should be specified on many instead of belongs_to.

Upvotes: 4

Related Questions