2014-12-08 74 views
1

這是不可能的,因爲它是正確的,但現在我有一個像這樣Neo4j的寶石 - 采薇多個節點/關係

events = Event.as(:e).where("((e.date_start - {current_time})/{one_day_p}) < 1 ").users(:u, :rel).where("rel.reminded = {reminded_p}").params(reminded_p: false, one_day_p: one_day, current_time: time).pluck(:e,:u, :rel)

我們的目標是獲得events,其中起始日期小於查詢一天之外。假設我試圖採取事件,用戶和關係。我需要對每個人採取不同的行動。

我需要爲每個event獲取所有users,併爲每個用戶執行電子郵件操作。在該行動中,電子郵件需要訪問該event

接下來,我需要將rel.reminded屬性更新爲true

是否有辦法同時採集所有這些以便能夠組織和執行這些任務?我開始單獨做這件事,但我必須做出額外的查詢才能實現。例如

events = Event.as(:e).where("((e.date_start - {current_time})/{one_day_p}) < 1 ").users(:u, :rel).where("rel.reminded = {reminded_p}").params(reminded_p: false, one_day_p: one_day, current_time: time).pluck(:e)

然後我不得不

events.each do |event| 
# do stuff 
end 

# do another query that is associated and do more stuff 

更新:

合併的答案,我有這樣的事情沒有清理時間的方法呢。我在一個搜索用戶的地方添加了,因爲我需要在稍後的電子郵件功能中考慮這一點。所以我不確定將它包含在查詢中比在每個用戶之外進行更有效。

@collection = Event.as(:e).where("((e.date_start - {current_time})/{one_day_p}) < 1 ").users(:u).where(setting_reminder: true).rel_where(reminded: false).params(one_day_p: one_day, current_time: time).pluck(:e, 'COLLECT(u)', 'COLLECT(rel)') 

但是rel未與此查詢一起定義。

回答

1

首先,如果您使用的是寶石版本v4,請使用rel_where而不是where,併爲您的關係使用字符串!

你應該能夠改變你的採摘爲pluck(:e, 'COLLECT(u)', 'COLLECT(rel)'),它會給你一個事件,用戶和rels的大量enumerable,所有事件都歸入父數組中。它會組織這樣的:

[ 
    [event1, [user1a, user1b, user1c], [rel1a, rel1b, rel1c]], 
    [event2, [user2a, user2b, user2c], [rel2a, rel2b, rel2c]] 
    ] 

每個相對的位置的用戶匹配,所以rel1a是事件和user1a之間的關係。

您可以將其設置爲@collection = my_big_query,然後在您的視圖中執行@collection.each do |event, users, rels|以循環瀏覽。您將對用戶執行each_with_index,並且用戶的索引將對應於rels中的位置。它看起來像這樣:

<%= @collection.each do |event, users, rels| %> 
    <%= event.name %> 
    <% users.each_with_index do |user, i| %> 
    <%= user.name %> said they were attending at <%= rels[i].confirmed_at %>.<br /> 
    <% end %> 
<% end %> 
+0

最後回到這個。使用類似於@collection = Event.as(:e).where(「((e.date_start - {current_time})/ {one_day_p})<1」的collect方法).users(:).rel_where(提醒:false).params(one_day_p:one_day,current_time:time).pluck(:e,'COLLECT(u)','COLLECT(rel)')它不知道集合中有什麼'rel'。它說'rel沒有定義'。我添加了一個編輯,以便將其更改爲 – Clam 2014-12-17 08:50:04

+0

'.users(:u,:rel)' – subvertallchris 2014-12-17 16:56:52

+0

您應該可以執行')/ {one_day_p})<{one}'並將'one:1'添加到您的參數也是如此。 : -/ – subvertallchris 2014-12-17 16:57:31

2

我也想在幾個點的鐘聲。您應該能夠使用ActiveSupport使查詢更容易這樣的一部分:

Event.as(:e).where("date_start < {date_threshold}").params(1.day.from_now) 

此外,爲了使它不那麼雜亂我沒有看到把.where("rel.reminded = false")(或使用rel_where(reminded: false)克里斯建議)任何問題。這不是從用戶傳入的數據,當您調用查詢時,它不會在不同的時間更改,所以它應該同樣高效。

您可能還需要使用新的查詢鏈接在創業板上市的V4定義unreminded_users方法是這樣的:

class Event 
    def self.unreminded_users 
    all.rel_where(reminded: false) 
    end 
end 

我其實並不想在一個查詢鏈做一個rel_where像,但我懷疑它會起作用。那麼你只是有這個:

Event.as(:e).where("e.start_date < {date_threshold}").params(date_threshold: 1.day.from_now) 
    .users(:u, :rel).rel_where(reminded: false) 
    .pluck(:e,:u, :rel) 
+0

要清楚,我仍然非常支持Chris提到的'collect'方法,我們希望實現預加載,以便可以通過對象方法調用在不久的將來 – 2014-12-08 13:40:25

+0

我終於回到了這個任務。我並沒有真正知道你添加的新查詢鏈接方法在哪裏,但我會嘗試查看文檔以瞭解更多關於它的信息 – Clam 2014-12-17 08:38:41

+0

這非常新穎(我認爲它進入了3.0.x的最新補丁版本,但它可能應該等到小版本或主版本)。基本上它只是讓你在各種查詢中重用查詢鏈的一部分。看看這篇關於ActiveRecord的博客帖子,它非常類似:http://blog.plataformatec.com.br/2013/02/active-record-scopes-vs-class-methods/ – 2014-12-17 10:44:04