2014-07-18 33 views
-1

我必須稱爲DraftPostPublishedPost相同的模型。我試圖做的是從特定網站上查找稿子和發佈的帖子,然後將它們合併在一起。合併ActiveRecord對象和他們的孩子在一起

draft_posts = DraftPost.where(:site_id => params[:site_id]).includes(:comments) 
#=> 3 draft posts 

published_posts = PublishedPost.where(:site_id => params[:site_id]).includes(:comments) 
#=> 2 published posts 

# here I want to merge the draft_posts to the published_posts. e.g; 
published_posts.merge(draft_posts) 
#=> here the result should be 3 published posts (update the existing two and create one new) 

你會如何做到這一點?謝謝。

+0

您是否嘗試過draft_post + published_post? – qcam

+1

您應該使用STI而不是創建兩個表格。 – Hauleth

+0

@hauleth:tnx,它是一個已經存在的項目,我需要繼續這個工作..現在不能改變它。 – tokhi

回答

1

嘗試:

published_posts << draft_posts.collect 
+0

那你怎麼保存呢? – tokhi

+0

未定義的方法'<<'for#<(table-obj name here)... – JosephK

0

使用+來連接兩個數組:

all_posts = published_posts + draft_posts 
+0

未定義的方法'+'for#<(table-obj name here)... – JosephK

相關問題