Rails 中的位置数据库模式表
我正在尝试在 Rails 应用程序中设计位置属性的架构。
这应该是正确的设计吗
class CreateLocations < ActiveRecord::Migration
def self.up
create_table :locations do |t|
t.string :zip
t.string :city, :null => false
t.string :state, :null => false
t.string :country, :null => false
t.string :latitude
t.string :longitude
t.timestamps
end
end
end
I am trying to design a schema for location attribute in my rails application.
Should this be the right design
class CreateLocations < ActiveRecord::Migration
def self.up
create_table :locations do |t|
t.string :zip
t.string :city, :null => false
t.string :state, :null => false
t.string :country, :null => false
t.string :latitude
t.string :longitude
t.timestamps
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这取决于您希望模型如何与您的表单集成。
我假设位置模型中的数据是由用户通过针对 Yahoo / Google 或静态邮政编码查找表对邮政编码进行地理编码来填充的。
无论如何,这是我的位置模型的迁移,我通过用户输入的邮政编码 (zip) 从静态查找表中填充 - 这是通过 AJAX 调用在主表单中填充的,如果可以的话,它会向 Google 发送地理编码请求找不到邮政编码(表格现在已经相当旧了),
我使用浮点数表示经度/纬度,尽管使用字符串可能同样好,甚至更好。
我对地区、国家和 Hpu(健康保护部门 - 英国国民医疗服务体系服务的一个区域部门)的地理细分使用了单独的模型和表格。
造成这种情况的原因有很多,包括将用户输入限制为下拉菜单,这些菜单仍然可以由管理员编辑(地区和国家是稳定的单位,但 Hpus 正在进行名称更改)。
其他原因包括扩展地理模型以包含附加信息,例如 shapefile 信息(用于渲染单位的边界 - 在此迁移中未显示,因为 shapefile 是稍后使用 PostGIS 导入工具添加的),以及使用这些附加模型提供的关联
I suppose it depends how you want the model to integrate with your form.
I'm assuming that the data in the Location model is populated by the user, via geocoding the zip against Yahoo / Google or from a static zipcode lookup table.
Any way here's the migration for my Location model, that I populate from a static lookup table via the user inputted postcode (zip) - this is populated in the main form by an AJAX call, which sends a geocode request off to Google if it can't find the postcode (the table is getting fairly old now)
I went with floating point numbers for the lon / lat although using a string is probably just as good if not better.
I used seperate models and tables for the geography subdivisions of Region, Country and Hpu (Health Protection Unit - a uk regional division for nhs services).
A number of reasons for this including restricting the user input for these to dropdown menus, which could still be edited by admins (Regions and Countries are stable units but the Hpus are undergoing name changes atm).
Other reasons included expanding the geography models to include additional information such as shapefile information (to render borders of the units - not shown in this migration because the shapefiles are added later using PostGIS import tools), and to use the associations provided by these additional models
除非有特殊原因您认为这不能解决您的解决方案,否则从语法上来说,它对我来说看起来是正确的。 Rails 的优点在于,如果不正确,您可以迁移数据模型以在需要时添加更多属性。
unless there is a particular reason you don't think this is solving your solution, syntactically it looks right to me. the beauty of rails is that if it isnt right you can migrate the data model to add more attributes as you find you need them.