假设我创建一个简单的特性的模型:(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