【问题标题】:Creating Association between mongodb and active record在 mongodb 和活动记录之间创建关联
【发布时间】:2015-02-27 07:06:11
【问题描述】:

例如,如何在 mongodb 模型和活动记录模型之间创建 has_many、has_and_belongs_to_many、belongs_to 等关联。我有一个使用 mongoid 的类 Item,我还有一个继承自 ActiveRecord 的类 ItemType

class Item
  include Mongoid::Document
  embeds_many :extra_fields, class_name: "ItemType"
end

class ItemType < ActiveRecord::Base
  belongs_to :item
end

谢谢

【问题讨论】:

标签: ruby-on-rails mongodb activerecord


【解决方案1】:

在 mongoDB 中,references_manyreferenced_in 等于 has_many 和 belongs_to。还有另一种方法可以使用embeds_manyembedded_in 在mongoDB 中实现has_many 和belongs_to 关系。

如果你想单独获取孩子的记录,那么你应该去references_manyreferenced_in。如果孩子总是只和父母一起来,那么你应该选择embeds_manyembedded_in

如果 embeds_manyembedded_in 子嵌套在 mongo 对象中与父级嵌套,而在 references_manyreferenced_in 的情况下,则子级和父级的不同 mongo 对象具有引用键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多