通过许多 :in => 在 Mongo Mapper 中存储有关文档的附加信息大批
我在泵和零件之间有多对多的关系。我将零件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您确实需要第三个模型,例如 PartUsage 或封装关系的东西。关键是您正在尝试存储和使用有关关系本身的一些数据,这需要另一个模型(EmbeddedDocument 是理想的选择)。
你可以这样做(注意:未经测试!):
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!):