Ruby-on-rails Rails:添加迁移以添加数组(默认为空)

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

Rails: Adding migration to add an array (default empty)

ruby-on-railspostgresql

提问by user2669464

I'm trying to add a column called share to one of my resources. The idea is that users can upload documents and share them with other (specific) users, and the array contains the emails of those that the user wants to share with.

我正在尝试向我的资源之一添加一个名为 share 的列。这个想法是用户可以上传文档并与其他(特定)用户共享,数组包含用户想要共享的电子邮件。

I tried adding a migration with the code

我尝试使用代码添加迁移

class AddShareToDocuments < ActiveRecord::Migration
  def change
    add_column :documents, :share, :array, :default => []
  end
end

But when I open up rails console in the command prompt, it says that share:nil and user.document.share.class is NilClass.

但是当我在命令提示符下打开 rails 控制台时,它说 share:nil 和 user.document.share.class 是 NilClass。

Creating a new array in the rails console sandbox by typing

通过键入在 rails 控制台沙箱中创建一个新数组

newarray = []

says that newarray.class is Array.

说 newarray.class 是数组。

Can anyone spot what I'm doing wrong?

谁能发现我做错了什么?

回答by gilcierweb

Rails 4 the PostgreSQL Array data type

Rails 4 PostgreSQL 数组数据类型

In terminal

在终端

$ rails generate migration AddTagsToProduct tags:string

Migration file:

迁移文件:

class AddTagsToProduct < ActiveRecord::Migration
  def change
    add_column :products, :tags, :string, array: true, default: []
  end
end

https://coderwall.com/p/sud9ja/rails-4-the-postgresql-array-data-type

https://coderwall.com/p/sud9ja/rails-4-the-postgresql-array-data-type

回答by michelemina

if you want support all databases you must serialize the array in a String

如果要支持所有数据库,则必须将数组序列化为字符串

class Documents < ActiveRecord::Base
 serialize :share
end

class AddShareToDocuments < ActiveRecord::Migration
 def change
   add_column :documents, :share, :string, :default => []
 end 
end

In case of Postgresql and array datatype I found https://coderwall.com/p/sud9ja

在 Postgresql 和数组数据类型的情况下,我发现https://coderwall.com/p/sud9ja

回答by Steve Y

Arrays are not normally a type to be stored in a database. As michelemina points out, you can serialize them into a string and store them, if the type of the data in the array is simple (strings, int, etc). For your case of emails, you could do this.

数组通常不是要存储在数据库中的类型。正如 michelemina 指出的,如果数组中的数据类型很简单(字符串、整数等),您可以将它们序列化为字符串并存储它们。对于您的电子邮件,您可以这样做。

If, on the other hand, you want to be able to find all of the User objects that a document was shared with, there are better ways of doing this. You will want a "join table". In your case, the join-table object may be called a Share, and have the following attributes:

另一方面,如果您希望能够找到与文档共享的所有 User 对象,则有更好的方法可以做到这一点。您将需要一个“连接表”。在您的情况下,连接表对象可能称为共享,并具有以下属性:

class Share
  belongs_to :user
  belongs_to :document
end

Then, in your Document class,

然后,在您的 Document 类中,

has_many :shares
has_many :users, :through => :shares

As far as generating the migration, this may be hacky, but you could create a new migration that changes the type to "string" (Edit: correct code):

至于生成迁移,这可能是hacky,但您可以创建一个新的迁移,将类型更改为“字符串”(编辑:正确的代码):

class AddShareToDocuments < ActiveRecord::Migration
  def up
    change_column :documents, :share, :string
  end
  def down
    change_column :documents, :share, :array, :default => []
  end
end