我有一個代碼模型和一個國家模型。每個列表都有一個國家(作爲其地址詳細信息的一部分),但每個列表還可以有多個ExportCountries(只是列表所有者出口到的國家/地區)。Rails,有一個國家和許多ExportCountries?
A listing has_one country
A Country has_many listings
A listing has_and_belongs_to_many ExportCountries
An ExportCountry has_and_belongs_to_many Listings
如果我有兩個獨立的模型,我可能會做到這一點:
class Listing < ActiveRecord::Base
belongs_to :country
has_and_belongs_to_many :export_countries
end
class Country < ActiveRecord::Base
has_many: listings
end
class ExportCountry < ActiveRecord::Base
has_and_belongs_to_many :listings
end
但是,我怎樣才能用一個國家模式來做到這一點 - 因爲否則ExportCountry將具有完全相同的記錄,這不是非常乾燥,並且看起來不像Rails那樣。
謝謝 - 它的工作原理! – A4J