2013-03-25 111 views
1

我有一個元素可以有一個或一些標題,一個或一些描述和一個或一些關鍵字列表。在網絡視圖中,我們有一種「添加替代標題」按鈕。我想知道在Mongoid中設計這種模式的正確方法是什麼。在Mongoid中處理多個標題,描述和關鍵字

我想實現這個3個解決方案(從最壞的可能選到最好的)之一:

1)添加字段到一個合理的數量

class Video 
    include Mongoid::Document 
    field :alt_title_1 
    field :alt_title_2 
    field :alt_title_3 
    field :alt_description_1 
    field :alt_description_2 
    field :alt_description_3 
    field :alt_keywords_1 
    field :alt_keywords_2 
    field :alt_keywords_3 
end 

2)或者,定義該字段類型:數組

class Video 
    include Mongoid::Document 
    field :titles, type: Array 
    field :descriptions, type: Array 
    field :keywords, type: Array 
end 

3)或者,具有複雜的關係

class Video 
    include Mongoid::Document 
    embeds_many :titles, :descriptions, :keywords 
end 

class Title 
    include Mongoid::Document 
    embedded_in :video 
    field :value 
end 

class Description 
    include Mongoid::Document 
    embedded_in :video 
    field :value 
end 

class Keyword 
    include Mongoid::Document 
    embedded_in :video 
    field :value, type: Array 
end 

您認爲更有意義嗎?那裏有更好的解決方案嗎?

回答

3

我個人最喜歡2。這些只是字符串,所以它有多個模型沒有意義。

+0

我明白了,你知道如何對數組進行驗證嗎?我只用於3)。 – Hartator 2013-03-26 00:58:09

0

因爲和傑夫相同的原因,我也會選擇2號。如果我必須驗證輸入,我會進行自定義驗證。

相關問題