通过许多 :in => 在 Mongo Mapper 中存储有关文档的附加信息大批

发布于 2024-08-21 01:31:47 字数 759 浏览 5 评论 0原文

我在泵和零件之间有多对多的关系。我将零件 ID 存储在泵文档中。我还想存储特定泵中使用了多少零件。

简而言之,这就是我所拥有的,

class Pump
  include MongoMapper::Document

  key :number, String
  key :desc, String
  key :part_ids, Array

  many :parts, :in => :part_ids 
end

class Part
  include MongoMapper::Document

  attr_accessor :pump_ids

  key :qty, Integer
  key :number, String
  key :desc, String

  def pumps
    Pump.all(:part_ids => self.id)
  end
end

在我意识到每个泵使用的零件数量不同之前,它工作得很好,所以现在我需要存储每个关系的数量,也许还有一些其他关系特定信息,如注释。

我不只是存储 ids 数组,而是想要这样的东西。

[{:pump_id => "12hj3hjkbrw", :qty = 4},
 {:pump_id => "ggtyh5ehjrw", :qty = 10, :notes => "when using this part with this Pump please note this"}]

我不知道如何进行这项工作。

I have a many to many relationship between Pumps and Parts. I am storing the Part ids in the Pump document. I would also like to store how many Parts are used in a particular Pump.

Here's what I have, in short

class Pump
  include MongoMapper::Document

  key :number, String
  key :desc, String
  key :part_ids, Array

  many :parts, :in => :part_ids 
end

class Part
  include MongoMapper::Document

  attr_accessor :pump_ids

  key :qty, Integer
  key :number, String
  key :desc, String

  def pumps
    Pump.all(:part_ids => self.id)
  end
end

Which worked fine until I realized the number of Parts used per Pump is different, So now I need store the qty per the relationship, and maybe some other relational specific info like notes.

Instead of just storing and Array of ids I'd like something like this instead.

[{:pump_id => "12hj3hjkbrw", :qty = 4},
 {:pump_id => "ggtyh5ehjrw", :qty = 10, :notes => "when using this part with this Pump please note this"}]

I am not sure how to make this work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2024-08-28 01:31:47

看来您确实需要第三个模型,例如 PartUsage 或封装关系的东西。关键是您正在尝试存储和使用有关关系本身的一些数据,这需要另一个模型(EmbeddedDocument 是理想的选择)。

你可以这样做(注意:未经测试!):

class Pump
  include MongoMapper::Document

  key :number, String
  key :desc, String
  key :part_usage_ids, Array

  many :part_usages
end

class Part
  include MongoMapper::Document

  key :qty, Integer
  key :number, String
  key :desc, String

  def pumps
    # not 100% sure of this query actually
    PartUsage.all(:part_id => self.id).collect { |pu| pu.pump }
  end
end

class PartUsage
  include MongoMapper::EmbeddedDocument

  belongs_to :pump
  belongs_to :part
  key :pump_id, ObjectId
  key :part_id, ObjectId
  key :qty, Integer
  key :notes, String
end

It looks like you really want a third model here, like PartUsage or something to encapsulate the relationship. The key is that you're trying to store and work with some data about the relationship itself, and that requires another model (an EmbeddedDocument is ideal).

You could do something like this (beware: untested!):

class Pump
  include MongoMapper::Document

  key :number, String
  key :desc, String
  key :part_usage_ids, Array

  many :part_usages
end

class Part
  include MongoMapper::Document

  key :qty, Integer
  key :number, String
  key :desc, String

  def pumps
    # not 100% sure of this query actually
    PartUsage.all(:part_id => self.id).collect { |pu| pu.pump }
  end
end

class PartUsage
  include MongoMapper::EmbeddedDocument

  belongs_to :pump
  belongs_to :part
  key :pump_id, ObjectId
  key :part_id, ObjectId
  key :qty, Integer
  key :notes, String
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文