Foreign Direct Investment (% GDP)
2000 2001 2002 2003 2004 2005 2006
Brazil 5.09 4.05 3.28 1.84 2.74 1.7 1.73
China 3.22 3.36 3.39 2.86 2.84 3.44 2.81
India 0.78 1.15 1.11 0.73 0.83 0.94 2.13
Mexico 2.87 4.43 3.37 2.35 3.11 2.57 2.01
我对Ruby on Rails应用程序有2个想法:
1)有3个型号:
2)处理大量数据的规范化程度较低但效率可能更高
这些都是抽象数据的好方法吗?你会使用哪种或者你会提出另一种方法?
谢谢!
答案 0 :(得分:1)
这取决于您希望系统的动态程度。您可以使用每个度量标准类型的一个类(和db表)来完成它,并通过Country.has_many:foreign_direct_investment_metrics等关联它们(更简单)。
或者你可以更有活力,我会在度量标准上使用单表继承,如下所示:
class Country < ActiveRecord::Base
has_many :data_points, :class_name => 'Metric'
has_many :foreign_direct_investments, :class_name => 'ForeignDirectInvestment'
end
class Metric < ActiveRecord::Base
belongs_to :country
end
class ForeignDirectInvestment < Metric
end
class OtherMetricType < Metric
end
# c = Country.new(:name => 'Brazil')
# c.data_points << ForeignDirectInvestment.new(:year => 2000, :value => 5.09)
# c.save
您的数据库架构:
create_table "countries", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "metrics", :force => true do |t|
t.integer "country_id"
t.string "type"
t.integer "year"
t.float "value"
t.datetime "created_at"
t.datetime "updated_at"
end
这使您可以向ForeignDirectInvestment添加方法,同时保持灵活记录更多指标。但是,ActiveRecord的STI确实有一些警告,例如所有模型的所有密钥都必须放在基本指标表上。这里有一个很好的STI介绍:http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/
您可能还想考虑像MongoDb这样的文档数据库。 MongoMapper是Mongo和Rails的简单数据映射器,它也使得解决这些问题变得更容易。见http://mongomapper.com/