2016-08-26 3 views
0

Ich verwende Edelstein und Mongoid in Ruby. Ich verbinde tatsächlich die Mongo-Geschichte mit einem Modell namens SocialPost, also kann ich so etwas machen.Erstellen Sie ein Modell Scope mit Hash-Attribut

history = current_user.social_posts.history_tracks 

Jetzt muss ich diese ‚Geschichte‘ filtern, mit einem Rahmen oder ein Verfahren, das Filterattribut ‚association_chain‘ aus der Geschichte Tracker-Modell, aber ‚history_tracks‘ Attribute auf diese Weise hergestellt werden:

<HistoryTracker _id: 57bdc1cb65e59325ae000001, created_at: 2016-08-24 15:48:27 UTC, updated_at: 2016-08-24 15:48:27 UTC, association_chain: [{"name"=>"SocialPost", "id"=>BSON::ObjectId('57ac8b0f65e5930944000000')}], modified: {"facebook_likes_count"=>2594213, "tweeter_followers_count"=>0}, original: {}, version: 1, action: "update", scope: "social_post", modifier_id: nil> 

Also, wie kann ich eine Suche in meinem HistoryTracker Modell erstellen, die mir erlauben, eine bestimmte Gruppe von ids innerhalb association_chain, so etwas zu suchen:

UPDATE $ elemMatch

#test case multiple social_id: empty result 
HistoryTracker.any_in({:association_chain.elem_match => [{name: 'SocialPost', :id.in => social_ids}] }) 

#test case 1 social_id: match result 
HistoryTracker.any_in({:association_chain.elem_match => [{name: 'SocialPost', :id => social_ids.first}] }) 

Antwort

0

Ich fand bereits eine posible Lösung. Die elemMatch sollte mit ids Sie sammeln müssen gruppiert werden gebaut, so ist dies, was ich tat:

social_ids = [BSON::ObjectId('...1'), BSON::ObjectId('...2'), BSON::ObjectId('...3')] 

#reorder the ids with the extra hash elements 
a = [] 
social_ids.each do |id_bson| 
    a << {name: 'SocialPost', id: id_bson} 
end 

Und Sie jetzt die ‚QUERY‘

HistoryTracker.any_in({:association_chain.elem_match => a}) 
erstellen
Verwandte Themen