在ruby上的DataMapper中声明关联?

在ruby上的DataMapper中声明关联?,ruby,sinatra,datamapper,Ruby,Sinatra,Datamapper,我需要创建一个包含以下表格的数据库:学校、地址、年份、类型和结果。 每个学校只能有一个地址和许多年,每年必须有许多类型,并且每年的每个类型只能有一个结果表 下面是我的DataMapper模型: class School include DataMapper::Resource property :id, Serial property :title, String, :length => 225, :required => true property :type,

我需要创建一个包含以下表格的数据库:学校、地址、年份、类型和结果。 每个学校只能有一个地址和许多年,每年必须有许多类型,并且每年的每个类型只能有一个结果表

下面是我的DataMapper模型:

class School
  include DataMapper::Resource

  property :id, Serial
  property :title, String, :length => 225, :required => true
  property :type, String

  has n, :years
  has 1, :address
  has n, :types, :through => :years
  has n, :visits
end

class Year
  include DataMapper::Resource

  property :id, Serial
  property :year, Integer

  has n, :types
  has n, :schools

end

class Result
  include DataMapper::Resource

  property :id, Serial
  property :subject, String, :length => 225
  property :total, Integer
  property :range_1, Integer
  property :range_2, Integer
  property :range_3, Integer
  property :range_4, Integer
  property :range_5, Integer
  property :range_6, Integer
  property :range_7, Integer
  property :range_8, Integer
  property :range_9, Integer
  property :range_10, Integer

  belongs_to :type
end

class Type
  include DataMapper::Resource

  property :id, Serial
  property :name, String, :length => 225, :required => true

  has n, :results
  has n, :years
  has n, :schools, :through => :years
end

class Address
  include DataMapper::Resource

  property :id, Serial
  property :Village, String
  property :City, String
  property :District, String
  property :State, String

  belongs_to :school
end
我找不到更好的方法来声明表之间的关联。也许你可以提出更好的建议


还有,我如何根据学校和学年选择具体的结果?

你说的更好的方法是什么意思?当前的实现并没有给您带来什么,您想要实现什么?您所说的更好的方式是什么意思?当前的实现没有给您带来什么,您想要实现什么?