mbigras mbigras - 4 years ago 188
Ruby Question

Initialize model with integer array in Postgres

I'm using postgres 9.6.1, Rails 4.2.0, and the following migration:

class CreateStageBatches < ActiveRecord::Migration
def change
create_table :stage_batches do |t|
t.text :item_ids, array: true, default: []

How do I create an array of integers? I've tried:

[9] pry(main)> StageBatch.new item_ids: [1,2,3]
=> #<StageBatch id: nil, item_ids: ["1", "2", "3"]>

But they are strings.

Looking at the postgres docs it looks like this is possible but I'm not sure what the syntax is for my migration or instantiation.

Answer Source

try this

 t.integer 'item_ids', array: true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download