38

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

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

Creating a new array in the rails console sandbox by typing

newarray = []

says that newarray.class is Array.

Can anyone spot what I'm doing wrong?

4
  • 1
    Are you using Postgres and want its array type?
    – Steve Y
    Jan 5, 2014 at 19:20
  • 1
    I believe I am using SQLite. I have PostgreSQL installed but I don't think I am using it for this application. I want to add a variable called "share" to "document" and I want "share"'s type to be an array. Jan 5, 2014 at 23:14
  • If either of these solutions worked for you, you should accept the answer that worked best. Same goes for any other questions you have asked. It makes it more clear to anyone else who runs into your question 6mo from now what worked and what didn't. If neither worked, but you figured it out yourself, you should post what you did and accept it.
    – Steve Y
    Jan 10, 2014 at 19:49
  • I haven't fully implemented either solution yet. There was another problem that came up that needed to be taken care of. And in the mean time, I am trying to figure out how to add a join table (since this concept is new to me). But rest assured, I will mark the answer that works for me. Thanks for the reminder Jan 11, 2014 at 18:51

3 Answers 3

126

Rails 4 the PostgreSQL Array data type

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

9

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

4
  • Thanks for this. But I just tried to rollback my last migration and I am getting the "undefined method 'to_sym' for nil class" error. I've been trying to google the solution to this to no avail. Any help on this? So I can't even undo my last migration to serialize the array. Jan 5, 2014 at 23:16
  • Can you delete the database? rake db:drop db:create then change your migration as I described and run rake db:migrate. Jan 6, 2014 at 9:12
  • rake db:drop doesn't work. "Couldn't drop db/development.sqlite3 : #<Errno::EACCES: Permission Denied" and "Couldn't drop db/test.sqlite3 : #<Errno::ENOENT: No such file or directory" Jan 6, 2014 at 13:55
  • @user2669464, To undo a migration, there are a couple ways. You can rewrite your migration with up and down. Or you can make a new migration to remove the column. rails g migration remove_share_from_documents - then a rake:db:migrate - And you can remove the migration file if you'd like with rails d migration remove_share_from_documents - and you can do the same with the original after you've removed what you wanted. rails d migration add_share_to documents
    – nil
    Feb 11, 2015 at 15:28
3

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.

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:

class Share
  belongs_to :user
  belongs_to :document
end

Then, in your Document class,

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):

class AddShareToDocuments < ActiveRecord::Migration
  def up
    change_column :documents, :share, :string
  end
  def down
    change_column :documents, :share, :array, :default => []
  end
end
2
  • Thanks for this idea. I'll give it a try once I get my migrations to work again. I didn't know about the :through association. When you mentioned changing the type to "string", were you referring to the class Share? In your migration code, I don't see how that turns it into a string type. Jan 6, 2014 at 14:01
  • Sorry, lazy adapting on my part. What I meant to show was edited in. Hope this works.
    – Steve Y
    Jan 7, 2014 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.