ruby-on-rails – Rails 4 – 将地址保存为数据库中的一列
发布时间:2020-12-17 04:02:35 所属栏目:百科 来源:网络整理
导读:我是rails的新手,也是一个简单的应用程序.我的ERD中有一个名为Client的模型,并希望保存每个客户端的地址. 我最初的想法是将地址保存为单独的字段,即: rails g model Client address_first:string address_second:string city:string ... 但这似乎效率很低.
我是rails的新手,也是一个简单的应用程序.我的ERD中有一个名为Client的模型,并希望保存每个客户端的地址.
我最初的想法是将地址保存为单独的字段,即: rails g model Client address_first:string address_second:string city:string ... 但这似乎效率很低.我确定必须有办法将地址保存为哈希,数组或者JSON?我已经阅读过并且人们一直在指使用“序列化”,但我不确定是否以及如何为地址字段实现此功能. 非常感激任何的帮助. 谢谢 解决方法
这取决于您使用的数据库–MySQL和Postgres都有JSON列类型 – Postgres也有一个名为hstore的哈希类型.
它们在hash / json列中也有不同的查询选项.虽然这些数据类型非常适合动态数据 – 但没有真正的性能提升(实际上可能更慢). 而且你不能对存储在hstore / json列中的属性使用对象映射,验证等所有的轨道优点. 当涉及到地址时,每个人都天真地将其置于用户(或客户端或客户)模型中,仅发现在现实世界中您需要多个地址. # == Schema Information # # Table name: clients # # id :integer not null,primary key # created_at :datetime not null # updated_at :datetime not null # class Client < ActiveRecord::Base has_many :addresses end # == Schema Information # # Table name: addresses # # id :integer not null,primary key # address_first :string # address_second :string # street :string # city :string # country :string # client_id :integer # created_at :datetime not null # updated_at :datetime not null # class Address < ActiveRecord::Base belongs_to :client end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |