街道名称和多个街道号码的正确模型

时间:2014-05-20 20:43:16

标签: ruby-on-rails-4 model

我是使用Rails的新手,我想知道如何正确设置这种类型的模型:

桌位

  • 街道名称
  • 街道号码
  • 街道号码
  • 街道号码
  • ...无限街道号码

我必须为每个街道名称保存几个街道号码。

最好的方法是什么?


更新:

@RoDoTiQ @Pavan感谢您的回答!你怎么看待这个?

class Street < ActiveRecord::Base
  # I try to mantain the "Standard Street" with one Street Number
end

class Custom_Street <  Street
  # Custom Street with principal Street Number and additional Street Numbers
  has_many :additional_street_numbers
end

class Additional_Street_Number < ActiveRecord::Base
  belongs_to :custom_street
end

2 个答案:

答案 0 :(得分:0)

您应该通过has_many运算符

执行Active记录关联

类似的东西:

class Street < ActiveRecord::Base
  has_many :street_numbers
end

class Street_Number < ActiveRecord::Base
  belongs_to :street
end

请查看此link了解详情。

答案 1 :(得分:0)

更相关的方式是has_many through

Class Place < ActiveRecord::Base

has_many :street_numbers
has_many :streets,through: :street_numbers

end

Class Street < ActiveRecord::Base

has_many :street_numbers
has_many :places,through: :street_numbers

end

Class StreetNumber < ActiveRecord::Base

belongs_to :place
belongs_to :street

end

有关详情,请参阅 Guides