假設我創建一個簡單的特性的模型:(Rails)我如何繞過STI空字段?
class CreateWingedThings < ActiveRecord::Migration
create_table :winged_things do
t.integer :number_of_wings
t.integer :species
t.integer :air_speed_velocity
t.boolean :laden, :default => false
end
end
但現在我想兩種特定類型的WingedThing
,用自己鮮明的特點:
class CreateBats < ActiveRecord::Migration
create_table :bats do
t.integer :echolocation_volume
t.string :snout_type
end
end
class CreateBirds < ActiveRecord::Migration
create_table :birds do
t.string :beak_size
t.integer :number_of_feathers
end
end
這是一個很簡單的事做STI - 有Bat
和Bird
繼承WingedThing
:
class Bat < WingedThing
(獎金問題:這在數據庫中看起來如何?我每Bat
和每個Bird
生成WingedThing
行嗎?這是我的理解,但請糾正我,如果我錯了。)
(從這可能錯誤的理解)但是,如果我想要,如果FlightlessBird
? air_speed_velocity
將是一個毫無意義的領域,它的負擔將是不道德的; FlightlessBird
的所有實例都將在爲相應的WingedThing
生成的DB行中包含空值條目laden
和air_speed_velocity
。這是比我需要的更多的數據,並不適合可擴展性,但我也不想完全刪除這些字段,因爲至少有兩個其他模型依賴於它們。
TL;博士我想什麼,能夠做的是有Bird
和Bat
能夠訪問兩者共同的特點,同時具有FlightlessBird
有同父表,但沒有產生一些特性數據庫中的空字段。
建模這樣的關係的最佳方式是什麼,以確保我可以獲得最薄的數據庫?
爲什麼migrations從'ActiveRecord :: Base'而不是從'ActiveRecord :: Migration'繼承? – mdesantis 2014-08-31 15:19:43
好問題。固定。 – Vardarac 2014-08-31 15:21:06