2

所以我有一個數據庫結構,看起來是這樣的:單表繼承還是多態?

活動:

event_id: :integer 
event_type: :string 
name: :string 
slug: :string 
user_id: :integer 
start: :datetime 
finish: :datetime 

和婚禮,我需要從事件繼承:

bride_name: :string 
groom_name: :string 
street: :string 
city: :string 
state: :string 
zip: :integer 
receptions: :text 

我希望能夠到創建婚禮或任何其他事件,並讓它繼承事件模型中所有事件中常見的特徵。當我試圖用STI來做這件事時,Wedding模型只具有Event模型的特徵。我如何實現這一點?

這不起作用

class Wedding < Event 

    attr_accessor :receptions, :reception_name, :reception_address, :reception_city, :reception_state, :reception_zip 

end 
+0

你在你的STI表中有一個'type'列來讓Rails保存事件的類型嗎?似乎在您的列表中缺失。 –

+0

是的,當我嘗試性病時,我做了。我列出的是我想要的結構。即使當我有一個類型列時,它也只是創建了一個包含事件模型屬性的對象 – watzon

+1

對於其中一個,您不應該需要任何'attr_accessor'語句來訪問模型中的屬性。它可以使閱讀更容易。有一個'type'列(具有適當的內容)並在Ruby代碼中繼承就足以讓STI工作。如果你想要MTI,你可以試試https://github.com/hzamani/active_record-acts_as –

回答

1

鐵軌只支持STI開箱即用,如果你真的想MTI,你可以給ActiveRecord::ActsAs寶石了一槍。

在他們的追蹤器中有幾個未解決的問題,但發展似乎有些活躍。