2012-07-20 56 views
1

我想添加一個has_many,它將返回不同類型的有序集合,類似於多態關聯。將多個模型合併到一個關聯中

class Group < AR:Base 
    has_many ?? 
end 

class Picture < AR::Base; belongs_to :group; end 
class Video < AR::Base; belongs_to :group; end 
class Audio < AR::Base; belongs_to :group; end 

我怎麼有組返回所有使用SQL這belong_to它的圖片,視頻和音頻。我真的不想訴諸使用Ruby,因爲我也想要訂購acts_as_list或類似的「媒體項目」集合。

+0

我不確定關於列表的行爲,但在行上應該可以使用連接表(多態)和包括。 – rubish 2012-07-20 18:23:10

回答

0

使用STI(單表繼承)。

class Group < AR:Base 
    has_many :media_items 
end 

class MediaItem < AR::Base; belongs_to :group; end 
class Picture < MediaItem; end 
class Video < MediaItem; end 
class Audio < MediaItem; end 

您只需要將type列添加到您的數據庫模式。

相關問題