Ruby-on-rails 在 Rails 中添加数组列

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

Add an array column in Rails

ruby-on-rails

提问by Chandra Shekhar

How do you declare an array column in Rails?

如何在 Rails 中声明数组列?

Detail

细节

I have the following model

我有以下型号

rails generate model User address:text

but I want a model which can store multiple addresses per user. The following declaration gives me an error

但我想要一个可以为每个用户存储多个地址的模型。以下声明给了我一个错误

rails generate model User address[]:text 

How do you declare an array column in Rails?

如何在 Rails 中声明数组列?

回答by shrikant1712

You can use following steps

您可以使用以下步骤

rails g migration add_subjects_to_book subjects:text

And the migration file:

和迁移文件:

class AddSubjectsToBook < ActiveRecord::Migration
  def change
    add_column :books, :subjects, :text, array: true, default: []
  end
end

We can check it now:

我们现在可以检查一下:

2.1.2 :001 > b = Book.create
   (0.2ms)  BEGIN
  SQL (2.0ms)  INSERT INTO "books" ("created_at", "updated_at") VALUES (, ) RETURNING "id"  [["created_at", "2014-10-17 08:21:17.870437"], ["updated_at", "2014-10-17 08:21:17.870437"]]
   (0.5ms)  COMMIT
 => #<Book id: "39abef75-56af-4ad5-8065-6b4d58729ee0", title: nil, created_at: "2014-10-17 08:21:17", updated_at: "2014-10-17 08:21:17", description: {}, metadata: {}, subjects: []>

2.1.2 :002 > b.subjects.class
 => Array

If you want to add array while creating table you can do as follows

如果要在创建表时添加数组,可以执行以下操作

create_table :products do |t|
  t.string :name, null: false
  t.references :category, null: false
  t.text :tags, array: true, default: []
end

回答by dimakura

If you are using Postgres, then this post http://blog.plataformatec.com.br/2014/07/rails-4-and-postgresql-arrays/suggests using array: trueoption in migration script:

如果您使用的是 Postgres,那么这篇文章http://blog.plataformatec.com.br/2014/07/rails-4-and-postgresql-arrays/建议array: true在迁移脚本中使用选项:

create_table :users do |t|
  # other columns
  t.text : addresses, array: true, default: []
end

In case you are not using Postgres, this answer might be helpful: Storing arrays in database : JSON vs. serialized array

如果您没有使用 Postgres,这个答案可能会有所帮助:Storing arrays in database : JSON vs. serialized array

回答by mahemoff

The examples using array: truewould require Postgres or some other database capable of an arrays type. For MySQL, use generic serialization, which lets you store any arbitrary type in a column.

使用的示例array: true需要 Postgres 或其他一些支持数组类型的数据库。对于 MySQL,使用通用序列化,它允许您在列中存储任意类型。

Database migration:

数据库迁移:

create_table :users do |t|
  t.text :addresses, default: [].to_yaml
  ...
end

Class with array attribute:

具有数组属性的类:

class User < ActiveRecord::Migration
  serialize :addresses, Array
end

Using the attribute:

使用属性:

u = User.new
u.update_attributes addresses: ["123 Evergreen", "246 Main"]

The usual caveats apply to storing arrays in a database. It goes against the grain of relational databases to do this and will make it hard, slow, or impossible to do things like search for an individual item. However, it can be a fine solution for basic storage until you need to do those things.

通常的警告适用于在数据库中存储数组。这样做与关系数据库的本质背道而驰,并且会使搜索单个项目等操作变得困难、缓慢或不可能。但是,在您需要做这些事情之前,它可能是基本存储的一个很好的解决方案。

回答by Rahul2692

If using Rails 5, follow the below steps to add an array of address in user table:

如果使用 Rails 5,请按照以下步骤在用户表中添加地址数组:

Run below command to generate a model with table,

运行以下命令以生成带有表的模型,

rails generate model User address:text

The table will get create similar to the given below:

该表将创建类似于下面给出的:

class CreateUser < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      t.text :address
      t.timestamps
    end
  end
end

Now add serialization for the address in a user model

现在为用户模型中的地址添加序列化

class User < ApplicationRecord
 serialize :address, Array
end

Thank you.

谢谢你。