Ruby-on-rails Rails 4:可用数据类型列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17918117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 4: List of available datatypes
提问by Nicolas Raoul
Where can I find a list of data types that can be used in Ruby on Rails 4? Such as
在哪里可以找到可在 Ruby on Rails 4 中使用的数据类型列表?如
textstringintegerfloatdate
textstringintegerfloatdate
I keep learning about new ones and I'd love to have a list I could easily refer to.
我一直在学习新的,我很想有一个我可以很容易地参考的清单。
回答by Nicolas Raoul
Here are all the Rails 4 (ActiveRecord migration) datatypes:
以下是所有 Rails 4(ActiveRecord 迁移)数据类型:
:binary:boolean:date:datetime:decimal:float:integer:bigint:primary_key:references:string:text:time:timestamp
:binary:boolean:date:datetime:decimal:float:integer:bigint:primary_key:references:string:text:time:timestamp
Source: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column
These are the same as with Rails 3.
来源:http: //api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column
这些与Rails 3相同。
If you use PostgreSQL, you can also take advantage of these:
如果您使用 PostgreSQL,您还可以利用这些:
:hstore:json:jsonb:array:cidr_address:ip_address:mac_address
:hstore:json:jsonb:array:cidr_address:ip_address:mac_address
They are stored as strings if you run your app with a not-PostgreSQL database.
如果您使用非 PostgreSQL 数据库运行应用程序,它们将存储为字符串。
Edit, 2016-Sep-19:
编辑,2016 年 9 月 19 日:
There's a lot morepostgres specific datatypes in Rails 4 and even morein Rails 5.
回答by lflores
You might also find it useful to know generally what these data types are used for:
您可能还会发现了解这些数据类型的一般用途很有用:
:string- is for small data types such as a title. (Should you choose string or text?):text- is for longer pieces of textual data, such as a paragraph of information:binary- is for storing data such as images, audio, or movies.:boolean- is for storing true or false values.:date- store only the date:datetime- store the date and time into a column.:time- is for time only:timestamp- for storing date and time into a column.(What's the difference between datetime and timestamp?):decimal- is for decimals (example of how to use decimals).:float- is for decimals. (What's the difference between decimal and float?):integer- is for whole numbers.:primary_key- unique key that can uniquely identify each row in a table
:string- 适用于小数据类型,例如标题。(你应该选择字符串还是文本?):text- 用于较长的文本数据,例如一段信息:binary- 用于存储图像、音频或电影等数据。:boolean- 用于存储真值或假值。:date- 仅存储日期:datetime- 将日期和时间存储到一列中。:time- 仅限时间:timestamp- 用于将日期和时间存储到一列中。(日期时间和时间戳之间有什么区别?):decimal- 用于小数(如何使用小数的示例)。:float- 用于小数。(十进制和浮点数有什么区别?):integer- 适用于整数。:primary_key- 可以唯一标识表中每一行的唯一键
There's also references used to create associations. But, I'm not sure this is an actual data type.
还有用于创建关联的引用。但是,我不确定这是一个实际的数据类型。
New Rails 4 datatypes available in PostgreSQL:
PostgreSQL 中可用的新 Rails 4 数据类型:
:hstore- storing key/value pairs within a single value (learn more about this new data type):array- an arrangement of numbers or strings in a particular row (learn more about it and see examples):cidr_address- used for IPv4 or IPv6 host addresses:inet_address- used for IPv4 or IPv6 host addresses, same as cidr_address but it also accepts values with nonzero bits to the right of the netmask:mac_address- used for MAC host addresses
:hstore- 在单个值中存储键/值对(了解有关这种新数据类型的更多信息):array- 特定行中的数字或字符串排列(了解更多信息并查看示例):cidr_address- 用于 IPv4 或 IPv6 主机地址:inet_address- 用于 IPv4 或 IPv6 主机地址,与 cidr_address 相同,但它也接受网络掩码右侧具有非零位的值:mac_address- 用于 MAC 主机地址
Learn more about the address datatypes hereand here.
Also, here's the official guide on migrations: http://edgeguides.rubyonrails.org/migrations.html
另外,这里是关于迁移的官方指南:http: //edgeguides.rubyonrails.org/migrations.html
回答by gotqn
It is important to know not only the types but the mapping of these types to the database types, too:
不仅要了解类型,还要了解这些类型到数据库类型的映射,这一点很重要:




Source added - Agile Web Development with Rails 4
添加了源代码 -使用 Rails 4 进行敏捷 Web 开发
回答by tomascharad
You can access this list everytime you want (even if you don't have Internet access) through:
您可以通过以下方式随时访问此列表(即使您没有 Internet 访问权限):
rails generate model -h
回答by Peter de Ridder
Rails4 has some added datatypes for Postgres.
Rails4 为 Postgres 添加了一些数据类型。
For example, railscast #400 names two of them:
例如, railscast #400 命名其中两个:
Rails 4 has support for native datatypes in Postgres and we'll show two of these here, although a lot more are supported: array and hstore. We can store arrays in a string-type column and specify the type for hstore.
Rails 4 支持 Postgres 中的原生数据类型,我们将在这里展示其中的两种,尽管支持更多:array 和 hstore。我们可以将数组存储在字符串类型的列中并指定 hstore 的类型。
Besides, you can also use cidr, inet and macaddr. For more information:
此外,您还可以使用 cidr、inet 和 macaddr。想要查询更多的信息:

