Mongomapper Foreign Key?

112 views
Skip to first unread message

Stuart Harrison

unread,
Aug 29, 2012, 11:02:13 AM8/29/12
to mongo...@googlegroups.com
Hi,

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, :foreign_key => :pkeyval
  many :notices, :foreign_key => :pkeyval
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?

Jon Kern

unread,
Aug 29, 2012, 11:22:43 AM8/29/12
to mongo...@googlegroups.com
just talking out loud without deep thinking or trying anything... or
looking at your code, so it might be total bunk...

have you noticed that an association has an object_id on the single side?

and, on the many side, you could even simply use an array of object IDs.

i think if you play around with a simple example classes and
associations, you will see some opportunity to do whatever you need.

if you still need help, ping the list.

sorry for being short, but i wanted to drop this idea your way sooner
rather than later so that you can act on it.

jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA

Stuart Harrison said the following on 8/29/12 11:02 AM:
> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en

Stuart Harrison

unread,
Aug 29, 2012, 11:24:49 AM8/29/12
to mongo...@googlegroups.com
Cool, thanks for that. Appreciated. I shall continue to have a play with that in mind :)

Jon Kern

unread,
Aug 29, 2012, 11:27:06 AM8/29/12
to mongo...@googlegroups.com
Stuart, here are some examples
https://github.com/JonKernPA/mongo_examples/tree/master/user_event

look at the user class
Stuart Harrison said the following on 8/29/12 11:24 AM:

Jon Kern

unread,
Aug 29, 2012, 11:27:25 AM8/29/12
to mongo...@googlegroups.com
i mean, look at the event class
Jon Kern said the following on 8/29/12 11:27 AM:

Stuart Harrison

unread,
Aug 29, 2012, 11:27:37 AM8/29/12
to mongo...@googlegroups.com
Brilliant. Thanks.

Stuart Harrison

unread,
Aug 30, 2012, 5:00:18 AM8/30/12
to mongo...@googlegroups.com
Just to pick up on this again, I think I've got this nailed now. I didn't need to specify the foreign key in the many part, and also had to override the primary key in my Activities object with the following:

key :_id, String

Cheers

Stuart

Jon Kern

unread,
Aug 31, 2012, 9:01:57 AM8/31/12
to mongo...@googlegroups.com
glad you worked it out!
Stuart Harrison said the following on 8/30/12 5:00 AM:
Reply all
Reply to author
Forward
0 new messages