Skip to content

AR not save updated PostgreSQL arrays #14763

Closed
@nleo

Description

@nleo
pry> model.array << element
pry>  model.save
   (0.4ms)  BEGIN
   (0.4ms)  COMMIT
=> true
unless File.exist?('Gemfile')
  File.write('Gemfile', <<-GEMFILE)
    source 'https://rubygems.org'
    gem 'rails', '4.1.0'
    gem 'pg'
  GEMFILE

  system 'bundle'
end

require 'bundler'
Bundler.setup(:default)

require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'pry'

ActiveRecord::Base.establish_connection adapter: "postgresql",
  host: "localhost",
  user: "postgres",
  database: "test"

ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  execute 'drop table if exists bars'
  create_table :bars
  execute "alter table bars add column array_of_varchar varchar(255)[]"
end

class Bar < ActiveRecord::Base
end

class BugTest < Minitest::Test
  def test_postgresql_save_array
    bar = Bar.create! array_of_varchar: []
    bar.array_of_varchar << 'Sample string'
    bar.save!
    bar.reload
    assert_equal 'Sample string', bar.array_of_varchar[0]
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions