使用同一张表实现:国家-省-市-县-镇 的级联
表名:locals
字段名 | 名称 | 类型 |
---|---|---|
name | 名称 | string |
type | 类型 | string |
parent_id | 父级 | integer |
abbr | 缩写 | string |
def path name
File.read("#{Rails.root}/db/#{name}")
end
# countries = YAML::load(path("country.json"))
countries = JSON.parse(path("country.json"))
n = 100000
countries.each do |key, value|
n= n+1
Local::Contuny.create(id: n, name: value, abbr: key ) unless Local::Contuny.find_by_abbr(key).present?
end
areas = JSON.parse(path("areas.json"))
streets = areas.values.flatten
contuny_id = Local::Contuny.find_by_abbr("CN").id
streets.each do |street|
id = street['id']
text = street['text']
abbr = Pinyin.t(text) { |letters| letters[0].upcase }
if id.size == 6 # 省市区
if id.end_with?('0000') # 省
Local::Province.create(name: text, id: id[0..1], abbr:abbr, parent_id: contuny_id ) unless Local.find_by_id(id[0..1]).present?
elsif id.end_with?('00') # 市
Local::City.create(name: text, id: id[0..3], abbr:abbr, parent_id: id[0..1] ) unless Local.find_by_id(id[0..3]).present?
else #区
Local::District.create(name: text, id: id, abbr:abbr, parent_id: id[0..3] ) unless Local.find_by_id(id).present?
end
else
Local::Street.create(name: text, id: id, abbr:abbr, parent_id: id[0..5] ) unless Local.find_by_id(id).present?
end
end
rake local:country rake local:area
rake local:init