3
(這不是我用實際的代碼,雖然這總結了什麼,我想要做的想法)(Rails的問題)合併多個多態性的has_many關係
class Connection < ActiveRecord::Base
belongs_to :connection1, :polymorphic => true
belongs_to :connection2, :polymorphic => true
end
class User < ActiveRecord::Base
has_many :followers, :class_name => 'Connection', :as => :connection1
has_many :followings, :class_name => 'Connection', :as => :connection2
end
我的問題是,我想了解我將如何創建一個名爲「網絡」的方法,以便返回的內容不是數組。像這樣,
u = User.first
u.network # this will return a merged version of :followings and :followers
所以,我還是可以做到這一點:
u.network.find_by_last_name("James")
ETA:
或者嗯,我想我的問題其實歸結到是否有可能創建一個合併2個has_many關聯的方法,這樣我仍然可以調用它的find_by方法。
這是什麼語言? – 2010-11-19 08:26:15
哎呀,對不起。這是針對Ruby on Rails的。 – odina 2010-11-19 08:28:08
你能否提供表格結構? 。我對這個問題不是很清楚......你在尋找一個自我加入嗎? – Rakesh 2010-11-19 10:09:57