Mongomapper外键?

时间:2012-08-29 14:42:02

标签: mongodb mongomapper padrino

我目前在Padrino项目中使用Mongomapper,我从外部源导入数据。主对象(Application)有两个关联的文档类型ActivityNotice

但是,我想自己指定外键,而不是使用内部Mongo ID,因为外键在数据导出中,我通过rake任务导入。

我尝试了EmbeddedDocument,但这会导致问题,因为我必须删除所有相关数据,而不是更新它,这是不理想的。

我尝试了以下内容,但没有运气:

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

我出错的任何想法?

1 个答案:

答案 0 :(得分:4)

MongoMappers的Associations documentation对此有点了解,但在test_associations功能测试中有一个例子。 :foreign_key定义应在many上指定,而不是belongs_to