Rails:添加迁移以添加数组(默认为空) [英] Rails: Adding migration to add an array (default empty)
问题描述
我正在尝试向我的资源之一添加一个名为share的列。
的想法是用户可以上传文档并与其他(特定)用户共享文档,并且该数组包含用户想要与之共享的电子邮件。
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.
我尝试使用代码添加迁移
I tried adding a migration with the code
class AddShareToDocuments < ActiveRecord::Migration
def change
add_column :documents, :share, :array, :default => []
end
end
但是当我在命令提示符,它说share:nil和user.document.share.class是NilClass。
But when I open up rails console in the command prompt, it says that share:nil and user.document.share.class is NilClass.
通过在rails控制台沙箱中键入
Creating a new array in the rails console sandbox by typing
newarray = []
说newarray.class是数组。
says that newarray.class is Array.
有人可以发现我在做什么错吗?
Can anyone spot what I'm doing wrong?
推荐答案
设置4种PostgreSQL数组数据类型
Rails 4 the PostgreSQL Array data type
在终端
$ rails generate migration AddTagsToProduct tags:string
迁移文件:
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
这篇关于Rails:添加迁移以添加数组(默认为空)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!