Rails 4 - 将地址保存为数据库中的一列 [英] Rails 4 - Save address as one column in database

查看:128
本文介绍了Rails 4 - 将地址保存为数据库中的一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的铁路和工作在一个简单的应用程序。我在我的ERD中有一个名为Client的模型,并希望为每个客户保存地址。



我最初的想法是将地址保存为单独的字段,例如:

  rails g model客户端address_first:字符串address_second:字符串city:string ... 

非常低效。我确定必须有一种方法来保存地址是散列,数组还是JSON?我已经阅读,人们一直在谈论使用序列化,但我不确定是否以及如何实现这个地址字段。



任何帮助将最感谢。



谢谢 解决方案

正在使用 - MySQL和Postgres都有JSON列类型 - Postgres也有一个名为hstore的哈希类型。



它们在查询hash / json列时也有不同的选项。虽然这些数据类型对于动态数据非常有用 - 但并没有真正的性能增益(它实际上可能会变慢)。



您无法使用存储在hstore / json列中的属性的对象映射,验证等的所有优点。



当涉及到地址时,每个人都会天真地将它放在那里用户(或客户或客户)模型仅用于发现在现实世界中您需要多个地址。

 #==模式信息

#表名:客户

#id:integer not null,主键
#created_at:datetime not null
#updated_at:datetime not null


class Client< ActiveRecord :: Base
has_many:地址
结束


#==模式信息

#表名:地址

#id:integer不为null,主键
#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地址< ActiveRecord :: Base
belongs_to:客户端
结束


I'm new to rails and working on a simple application. I have a model called Client in my ERD and would like to save the address for each client.

My initial thoughts where to save the address as seperate fields i.e:

rails g model Client address_first:string address_second:string city:string ...

but this seems very inefficient. I'm certain there must be a way to save the address s a hash, array or perhaps JSON? I've read around and people keep referring to using "serialise" but I'm uncertain if and how I would implement this for an address field.

Any help would be most appreciated.

Thanks

解决方案

It depends on what database you are using - both MySQL and Postgres have JSON column types - Postgres also has a hash type called hstore.

They also have different options for querying inside the hash/json column. While these data types are great for dynamic data - there is no real performance gain (it might actually be slower).

And you can't use all the rails goodness of object mapping, validations etc for attributes stored inside a hstore/json column.

When it comes to addresses everybody naively puts it in there User (or Client or Customer) model only to discover that in the real world you need multiple addresses.

# == 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

这篇关于Rails 4 - 将地址保存为数据库中的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆