Why Can't I Create an Array as a Column in a Table in Rails

Why can't I create an array as a column in a table in Rails?

Check out the Rails guide on associations (pay particular attention to has_many).

You can use any column type supported by your database (use t.column instead of t.type), although if portability across DBs is a concern, I believe it's recommended to stick to the types explicitly supported by activerecord.

It seems kind of funny for fruit to have_many apples, but maybe that is just an example? (I would expect apples to be a subclass of fruit).

Add an array column to a resource

You have to create new migration so rails g migration change_column_type_of_ids_and_length . Then edit generated migration file.

  1. First try to use change_column method. If this works, your data will be preserved. Else, try step 2

    change_column :streams , :ids , :string , array: true , default: []
    change_column :streams , :lengths, :integer ,array: true , default: []
  2. Here we are removing the column so the data , then creating new one.

    remove_column :streams, :ids
    remove_column :streams, :lengths
    add_column :streams , :ids , :string ,array: true , default: []
    add_column :streams , :lengths , :integer ,array: true , default: []

Ruby on Rails - Adding arrays to column

If you're using Postgresql you can simply use the update_all method to update array columns in your model.

Playlist.where(id: 1).update_all(songs_ids: [1,2])

Note it won't work with Playlist.find(1)....

Using Mysql you can consider to serialize to do this you must use a string type column.

 def change
add_column :playlists, :songs_ids, :string
end

Then specify the attribute which to serialize in your model.

class Playlist < ActiveRecord::Base 
serialize :songs_ids, Array
end

Then you can test pushing any value to it.

playlist = Playlist.first
playlist.songs_ids << 1
=> [1]
playlist.save
playlist.songs_ids << 2
=> [2]
playlist.save
playlist.songs_ids
=> [1, 2]

How to get an array with column names of a table

Suppose you have a Post model:

Post.column_names
# or
Post.columns.map { |column| column.name }

It will return an array with column names of the table 'posts'.

Problems with SQLITE columns (array) in Rails

As pointed above by @spickermann, seems like what you need is a has and belongs to many associations (HBTM) in both models users and coins, I think this approach is a better fit for your problem, is easy to get/update/create any data with this approach, at least I think is better than an array inside a column of your users table, in case you want to give it a try here is how I will do it:

So on your coin model add the following line:
has_and_belongs_to_many :users;
and in your user model: has_and_belongs_to_many :coins.

Once you added the proper associations to your models you need to generate a migration in order to create your join table, the code in the migration should look like below:

create_join_table :users, :coins do |t|
t.index [:user_id, :coin_id]
t.index [:coin_id, :user_id]
end

The example above is on rails 5.2, and with the above, you can do stuff like:

user1.coins << coin_a
user1.save

Then you can iterate over user1.coins if you want all the coins related to a particular user (This link might be helpful).

Hope the above helps !

How to get table as array of arrays where each sub array is a column (not a row)

This one works, calls SELECT n times (n = Mymodel.column_names.count)

my_array = Mymodel.column_names.map { |column| Mymodel.all.map(&column.to_sym) }

This one will call SELECT column_name instead of SELECT * n times

my_array = Mymodel.column_names.map { |column| Mymodel.pluck(column.to_sym) }

Better and quicker solution is this answer

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

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



Related Topics



Leave a reply



Submit