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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:25:12  来源:igfitidea点击:

Rails 4: List of available datatypes

ruby-on-railsrubyruby-on-rails-4rails-activerecord

提问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 中使用的数据类型列表?如

  • text
  • string
  • integer
  • float
  • date
  • text
  • string
  • integer
  • float
  • date

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.

Rails 4 中有更多特定于 postgres 的数据类型,Rails 5 中甚至更多

回答by lflores

You might also find it useful to know generally what these data types are used for:

您可能还会发现了解这些数据类型的一般用途很有用:

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

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:

不仅要了解类型,还要了解这些类型到数据库类型的映射,这一点很重要:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明



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。想要查询更多的信息:

https://blog.engineyard.com/2013/new-in-rails-4

https://blog.engineyard.com/2013/new-in-rails-4