0

如何合併這兩個ActiveRecord關係?結合兩處查詢

activity1 = PublicActivity::Activity.where("user_recipients LIKE ':id,%' or user_recipients LIKE '%, :id' or user_recipients LIKE '%, :id,%' or user_recipients = ':id'", id: current_user.id) 

activity2 = PublicActivity::Activity.where(recipient_id: current_user.id, recipient_type: 'User') 

當我做到這一點,如果是空的,他們默認爲空單

@activity = activity1.merge(activity2) 
+0

你的意思是鏈接'where'查詢? – Pavan

+0

[組合兩個ActiveRecord :: Relation對象]的可能重複(http://stackoverflow.com/questions/9540801/combine-two-activerecordrelation-objects) – Drenmi

回答

2

很簡單,爲什麼合併是返回空數組這是因爲它使用的查詢之間AND試試這個:

@activity = (activity1 + activity2)