加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Mongoid embeds / has_many:通过等价物

发布时间:2020-12-17 02:52:43 所属栏目:百科 来源:网络整理
导读:在Mongoid中,我如何实现与ActiveRecord相同的功能:through =提供? class Advertiser include Mongoid::Document embeds_many :campaigns # how would I do this embeds_many :advertisements,:through = :campaignsendclass Campaign embedded_in :adverti
在Mongoid中,我如何实现与ActiveRecord相同的功能:through =>提供?

class Advertiser
    include Mongoid::Document
    embeds_many :campaigns

    # how would I do this
    embeds_many :advertisements,:through => :campaigns

end

class Campaign
    embedded_in :advertiser
    embeds_many :advertisements
end

class Advertisement
    embedded_in :campaign

    # or this?
    embedded_in :advertiser,:through => :campaign
end

然后就能做到
Advertiser.first.advertisements

Advertisement.first.advertiser

Advertiser.campaigns.collect {|运动| campaign.advertisement}不是一个选项

如何,我将如何使用references_many / referenced_in执行这些操作?

解决方法

简短的回答是,你做不到. MongoDB没有连接表的概念,也没有一般的连接. Mongoid多对多“模拟”是通过在每一侧存储外键数组来完成的.

回应评论:MongoDB是一个文档存储.因此,它适用于“文档”高度异构的情况.当您为广告商存储Campains和广告子树时,您必须以ruby代码收集广告商的广告.如果您的数据具有非常同质的形式,那么您可以考虑使用关系数据库.我们经常使用MySQL来关联对象,然后将MongoDB文档添加到对象中,以便它们可以扩展.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读