Ruby on rails学习:Rails 的 active_record 数据表的常见类型

数据库的常见类型 Rails 4/5/6/7: List of available datatypes
更新于: 2021-12-31 12:28:16

Supported Types

Active Record supports the following database column types:

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:

  • :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 here and here.

Also, here's the official guide on migrations: http://edgeguides.rubyonrails.org/migrations.html

 

特殊的类型:references

I'm taking a guess that you're referring to t.references :associated_model in a migration?

Suppose two models, Post and Author.

class Post < ActiveRecord::Base
  belongs_to :author
end

class Author < ActiveRecord::Base
  has_many :posts
end

Your migration contains:

create_table :posts do |t|
  t.references :author
end

参考