Mongoid inserts / has _many: via equivalent

In Mongoid, how would you achieve the same as 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 

you can do Advertiser.first.advertisements as well as Advertisement.first.advertiser

Advertiser.campaigns.collect{|campaign| campaign.advertisement} Advertiser.campaigns.collect{|campaign| campaign.advertisement} not an option

How, how would I do this with references_many / referenced_in ?

+4
source share
1 answer

The short answer is that you cannot. MongoDB has no concept of a join table and does not join at all. Multimodel Mongol modeling is performed by storing arrays of foreign keys on each side.

In response to a comment: MongoDB is a document repository. Therefore, it is suitable for situations where the "documents" are very heterogeneous. When you store your advertisers with the Campains subtree and ads, you will have to collect advertisements for advertisers in the ruby ​​code. If your data is very uniform, you can use a relational database instead. We often used MySQL to link objects, and then added MongoDB documents to objects so that they are extensible.

+8
source

All Articles