Ruby-on-rails Rails 3 数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4685009/
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-03 00:05:10  来源:igfitidea点击:

Rails 3 datatypes?

ruby-on-railsrubyruby-on-rails-3rails-activerecord

提问by Elliot

Where can I find a list of data types that can be used in rails 3? (such as text, string, integer, float, date, etc.?) I keep randomly learning about new ones, but I'd love to have a list I could easily refer to.

在哪里可以找到可在 rails 3 中使用的数据类型列表?(例如文本、字符串、整数、浮点数、日期等?)我一直在随机学习新的,但我希望有一个我可以轻松参考的列表。

回答by Nicolas Raoul

Here are all the Rails3(ActiveRecord migration) datatypes:

以下是所有Rails3(ActiveRecord 迁移)数据类型:

:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:primary_key
:references
:string
:text
:time
:timestamp

:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:primary_key
:references
:string
:text
:time
:timestamp

Source

来源

回答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

在此处输入图片说明

For, example, note that in MS SQL Server we are using:

例如,请注意在 MS SQL Server 中我们使用的是:

  1. the old "datetime" instead "datetime2"
  2. decimal with its default precision
  3. text and varchar instead nvarchar
  4. int (not possible to use tiny int/small int/big int)
  5. image instead BLOB
  1. 旧的“datetime”而不是“datetime2”
  2. 使用默认精度的十进制数
  3. text 和 varchar 代替 nvarchar
  4. int(不能使用 tiny int/small int/big int)
  5. 图像代替 BLOB

回答by Tim Stephenson

Do you mean for defining active record migrations? or do you mean Ruby data types?

您的意思是定义活动记录迁移吗?或者你的意思是Ruby数据类型?

Here's a link that may help for creating migrations:

这是一个可能有助于创建迁移的链接:

Orthogonal Thought - MySQL and Ruby on Rails datatypes

正交思想 - MySQL 和 Ruby on Rails 数据类型

回答by lflores

It might be helpful to know generally what these data types are used for:

大致了解这些数据类型的用途可能会有所帮助:

  • 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.
  • decimal - is for 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 - 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.
  • time - is for time only
  • timestamp - for storing date and time into a column.
  • 二进制 - 用于存储图像、音频或电影等数据。
  • boolean - 用于存储真值或假值。
  • 日期 - 仅存储日期
  • datetime - 将日期和时间存储到一列中。
  • 十进制 - 用于小数。
  • float - 用于小数。(十进制和浮点数有什么区别?
  • integer - 用于整数。
  • primary_key - 可以唯一标识表中每一行的唯一键
  • 字符串 - 用于小数据类型,例如标题。(你应该选择字符串还是文本?
  • text - 用于较长的文本数据,例如一段信息。
  • 时间 - 仅用于时间
  • 时间戳 - 用于将日期和时间存储到列中。

I hope that helps someone! Also, here's the official list: http://guides.rubyonrails.org/migrations.html#supported-types

我希望能帮助别人!另外,这里是官方列表:http: //guides.rubyonrails.org/migrations.html#supported-types